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 function workgeneric ($config, $dn= NULL, $parent= NULL)
68 {
69 $tmp = search_config($config->data,"faiManagement","CLASS");
70 if(!empty($tmp)){
71 $this->fai_activated = TRUE;
72 }
74 plugin::plugin ($config, $dn, $parent);
75 $this->netConfigDNS = new termDNS($this->config,$this->dn,$this->objectclasses);
77 /* Read arrays */
78 foreach (array("ghNetNic", "ghIdeDev", "ghScsiDev") as $val){
79 if (!isset($this->attrs[$val])){
80 continue;
81 }
82 for ($i= 0; $i<$this->attrs[$val]['count']; $i++){
83 array_push($this->$val, $this->attrs[$val][$i]);
84 }
85 }
87 /* Create used ntp server array */
88 $this->gotoNtpServer= array();
89 if(isset($this->attrs['gotoNtpServer'])){
90 $this->inheritTimeServer = false;
91 unset($this->attrs['gotoNtpServer']['count']);
92 foreach($this->attrs['gotoNtpServer'] as $server){
93 $this->gotoNtpServer[$server] = $server;
94 }
95 }
97 /* Set inherit checkbox state */
98 if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer) == 0)){
99 $this->inheritTimeServer = true;
100 $this->gotoNtpServer=array();
101 }
103 /* Create available ntp options */
104 $tmp = $this->config->data['SERVERS']['NTP'];
105 $this->gotoNtpServers = array();
106 foreach($tmp as $key => $server){
107 if($server == "default") continue;
108 $this->gotoNtpServers[$server] = $server;
109 }
111 $this->modes["active"]= _("Activated");
112 $this->modes["locked"]= _("Locked");
114 /* Set base */
115 if ($this->dn == "new"){
116 $ui= get_userinfo();
117 $this->base= dn2base($ui->dn);
118 } else {
119 $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
120 }
122 /* Create an array of all Syslog servers */
123 $tmp = $this->config->data['SERVERS']['SYSLOG'];
124 foreach($tmp as $server){
125 $visible = $server;
126 if($server == "default") {
127 $visible = "["._("inherited")."]";
128 }
129 $this->gotoSyslogServers[$server] = $visible;
130 }
132 /* Save 'dn' for later referal */
133 $this->orig_dn= $this->dn;
134 }
137 function set_acl_base($base)
138 {
139 plugin::set_acl_base($base);
140 $this->netConfigDNS->set_acl_base($base);
141 }
143 function set_acl_category($cat)
144 {
145 plugin::set_acl_category($cat);
146 $this->netConfigDNS->set_acl_category($cat);
147 }
149 function execute()
150 {
151 /* Call parent execute */
152 plugin::execute();
154 if($this->is_account && !$this->view_logged){
155 $this->view_logged = TRUE;
156 new log("view","workstation/".get_class($this),$this->dn);
157 }
159 /* Do we need to flip is_account state? */
160 if(isset($_POST['modify_state'])){
161 if($this->is_account && $this->acl_is_removeable()){
162 $this->is_account= FALSE;
163 }elseif(!$this->is_account && $this->acl_is_createable()){
164 $this->is_account= TRUE;
165 }
166 }
168 if ((isset($_POST['action'])) && ($this->acl_is_writeable("FAIstate"))){
169 $cmd= search_config($this->config->data['TABS'], "workgeneric", "ACTIONCMD");
170 if ($cmd == ""){
171 print_red(_("No ACTIONCMD definition found in your gosa.conf"));
172 } else {
173 exec ($cmd." ".$this->netConfigDNS->macAddress." ".escapeshellarg($_POST['saction']), $dummy, $retval);
174 if ($retval != 0){
175 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
176 } elseif ($_POST['saction'] != "wake") {
178 /* Set FAIstate */
179 if($this->fai_activated && $this->dn != "new"){
180 $ldap = $this->config->get_ldap_link();
181 $ldap->cd($this->config->current['BASE']);
182 $ldap->cat($this->dn,array("objectClass"));
183 $res = $ldap->fetch();
185 $attrs = array();
186 $attrs['FAIstate'] = $this->FAIstate;
187 if(isset($this->mapActions[$_POST['saction']]) && $this->mapActions[$_POST['saction']] != ""){
188 $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
189 }
191 for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
192 $attrs['objectClass'][] = $res['objectClass'][$i];
193 }
195 if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
196 $attrs['objectClass'][] = "FAIobject";
197 }
199 if($attrs['FAIstate'] == ""){
200 #FIXME we should check if FAIobject is not used anymore
201 $attrs['FAIstate'] = array();
202 }
204 $ldap->cd($this->dn);
205 $ldap->modify($attrs);
206 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic (FAIstate) with dn '%s' failed."),$this->dn));
208 }
209 $this->didAction= TRUE;
210 }
211 }
212 }
214 /* Do we represent a valid terminal? */
215 if (!$this->is_account && $this->parent == NULL){
216 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
217 _("This 'dn' has no workstation features.")."</b>";
218 return($display);
219 }
221 /* Base select dialog */
222 $once = true;
223 foreach($_POST as $name => $value){
224 if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_writeable("base")){
225 $once = false;
226 $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
227 $this->dialog->setCurrentBase($this->base);
228 }
229 }
231 /* Dialog handling */
232 if(is_object($this->dialog)){
233 /* Must be called before save_object */
234 $this->dialog->save_object();
236 if($this->dialog->isClosed()){
237 $this->dialog = false;
238 }elseif($this->dialog->isSelected()){
240 /* A new base was selected, check if it is a valid one */
241 $tmp = $this->get_allowed_bases();
242 if(isset($tmp[$this->dialog->isSelected()])){
243 $this->base = $this->dialog->isSelected();
244 }
246 $this->dialog= false;
247 }else{
248 return($this->dialog->execute());
249 }
250 }
252 /* Add new ntp Server to our list */
253 if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers'])) && $this->acl_is_writeable("gotoNtpServer")){
254 $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
255 }
257 /* Delete selected NtpServer for list of used servers */
258 if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected'])) && $this->acl_is_writeable("gotoNtpServer")){
259 foreach($_POST['gotoNtpServerSelected'] as $name){
260 unset($this->gotoNtpServer[$name]);
261 }
262 }
264 /* Fill templating stuff */
265 $smarty= get_smarty();
267 /* Set acls */
268 $tmp = $this->plInfo();
269 foreach($tmp['plProvidedAcls'] as $name => $translation){
270 $smarty->assign($name."ACL",$this->getacl($name));
271 }
273 $smarty->assign("cn", $this->cn);
274 $smarty->assign("l", $this->l);
275 $smarty->assign("bases", $this->get_allowed_bases());
276 $smarty->assign("staticAddress", "");
278 $tmp = array();
279 foreach($this->gotoNtpServers as $server){
280 if(!in_array($server,$this->gotoNtpServer)){
281 $tmp[$server] = $server;
282 }
283 }
284 $smarty->assign("gotoNtpServers",$tmp);
286 /* Check if workstation is online */
287 $query= "fping -q -r 1 -t 500 ".$this->cn;
288 exec ($query, $dummy, $retval);
290 /* Offline */
291 if ($retval == 0){
292 $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
293 "instant_update" => _("Instant update"),
294 "update" => _("Scheduled update"),
295 "reinstall" => _("Reinstall"),
296 "rescan" => _("Rescan hardware"),
297 "memcheck" => _("Memory test"),
298 "localboot" => _("Force localboot"),
299 "sysinfo" => _("System analysis")));
300 } else {
301 $smarty->assign("actions", array("wake" => _("Wake up"),
302 "reinstall" => _("Reinstall"),
303 "update" => _("Scheduled update"),
304 "memcheck" => _("Memory test"),
305 "localboot" => _("Force localboot"),
306 "sysinfo" => _("System analysis")));
307 }
308 /* Arrays */
309 $smarty->assign("modes", $this->modes);
310 $smarty->assign("nfsservers", $this->config->data['SERVERS']['NFS']);
311 $smarty->assign("syslogservers", $this->gotoSyslogServers);
312 $smarty->assign("fai_activated",$this->fai_activated);
314 $ntpser = array();
315 foreach($this->gotoNtpServers as $server){
316 if(!in_array($server,$this->gotoNtpServer)){
317 $ntpser[$server] = $server;
318 }
319 }
320 $smarty->assign("gotoNtpServers", $ntpser);
322 /* Variables */
323 foreach(array("base", "gotoMode", "gotoSyslogServer", "gotoNtpServer") as $val){
324 $smarty->assign($val."_select", $this->$val);
325 }
327 /* tell smarty the inherit checkbox state */
328 $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
330 /* Show main page */
331 $smarty->assign("netconfig", $this->netConfigDNS->execute());
332 return($smarty->fetch (get_template_path('workstation.tpl', TRUE)));
333 }
335 function remove_from_parent()
336 {
337 if($this->acl_is_removeable()){
339 $this->netConfigDNS->remove_from_parent();
340 $ldap= $this->config->get_ldap_link();
341 $ldap->rmdir($this->dn);
342 new log("remove","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
343 show_ldap_error($ldap->get_error(), sprintf(_("Removing of system workstation/generic with dn '%s' failed."),$this->dn));
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=".$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 }
356 }
357 }
360 /* Save data to object */
361 function save_object()
362 {
364 /* Create a base backup and reset the
365 base directly after calling plugin::save_object();
366 Base will be set seperatly a few lines below */
367 $base_tmp = $this->base;
368 plugin::save_object();
369 $this->base = $base_tmp;
371 /* Save base, since this is no LDAP attribute */
372 $tmp = $this->get_allowed_bases();
373 if(isset($_POST['base'])){
374 if(isset($tmp[$_POST['base']])){
375 $this->base= $_POST['base'];
376 }
377 }
379 $this->netConfigDNS->save_object();
381 /* Set inherit mode */
382 if((isset($_POST['workgeneric_posted'])) && ($this->acl_is_writeable("gotoNtpServer"))){
383 if(isset($_POST["inheritTimeServer"])){
384 $this->inheritTimeServer = true;
385 }else{
386 $this->inheritTimeServer = false;
387 }
388 }
390 }
393 /* Check supplied data */
394 function check()
395 {
396 /* Call common method to give check the hook */
397 $message= plugin::check();
399 /* Skip IP & Mac checks if this is a template */
400 if($this->cn != "wdefault"){
401 $message= array_merge($message, $this->netConfigDNS->check());
402 }
404 $this->dn= "cn=".$this->cn.",ou=workstations,ou=systems,".$this->base;
406 if ($this->cn == ""){
407 $message[]= _("The required field 'Workstation name' is not set.");
408 }
410 if ($this->orig_dn != $this->dn){
411 $ldap= $this->config->get_ldap_link();
412 $ldap->cd ($this->base);
414 if($this->cn == "wdefault"){
415 $ldap->cat($this->dn);
416 }else{
417 $ldap->search ("(&(cn=".$this->cn.")(objectClass=gotoWorkstation))", array("cn"));
418 }
419 if ($ldap->count() != 0){
420 while ($attrs= $ldap->fetch()){
421 if (preg_match ("/,ou=incoming,/", $ldap->getDN())){
422 continue;
423 } else {
424 if ($attrs['dn'] != $this->orig_dn){
425 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
426 break;
427 }
428 }
429 }
430 }
431 }
433 /* Check for valid ntpServer selection */
434 if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
435 $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
436 }
438 return ($message);
439 }
442 /* Save to LDAP */
443 function save()
444 {
445 plugin::save();
447 /* Strip out 'default' values */
448 foreach (array("gotoSyslogServer") as $val){
450 if (isset($this->attrs[$val]) && $this->attrs[$val] == "default"){
451 $this->attrs[$val]= array();
452 }
453 }
455 /* Add missing arrays */
456 foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
457 if (isset ($this->$val) && count ($this->$val) != 0){
458 $this->attrs["$val"]= $this->$val;
459 }
460 }
462 /* Remove all empty values */
463 if ($this->orig_dn == 'new'){
464 $attrs= array();
465 foreach ($this->attrs as $key => $val){
466 if (is_array($val) && count($val) == 0){
467 continue;
468 }
469 $attrs[$key]= $val;
470 }
471 $this->attrs= $attrs;
472 }
474 /* Update ntp server settings */
475 if($this->inheritTimeServer){
476 if($this->is_new){
477 if(isset($this->attrs['gotoNtpServer'])){
478 unset($this->attrs['gotoNtpServer']);
479 }
480 }else{
481 $this->attrs['gotoNtpServer'] = array();
482 }
483 }else{
484 /* Set ntpServers */
485 $this->attrs['gotoNtpServer'] = array();
486 foreach($this->gotoNtpServer as $server){
487 $this->attrs['gotoNtpServer'][] = $server;
488 }
489 }
491 if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
492 $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
493 }
495 /* Write back to ldap */
496 $ldap= $this->config->get_ldap_link();
497 if ($this->orig_dn == 'new'){
498 $ldap->cd($this->config->current['BASE']);
499 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
500 $ldap->cd($this->dn);
501 $ldap->add($this->attrs);
502 new log("create","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
503 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic with dn '%s' failed."),$this->dn));
504 if(!$this->didAction){
505 $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
506 }
507 } else {
508 if ($this->orig_dn != $this->dn){
509 $this->move($this->orig_dn, $this->dn);
510 }
511 $ldap->cd($this->dn);
512 $this->cleanup();
513 $ldap->modify ($this->attrs);
514 new log("modify","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
516 if(!$this->didAction){
517 $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
518 }
519 }
521 /* cn=default and macAddress=- indicates that this is a template */
522 if($this->cn == "wdefault"){
523 $this->netConfigDNS->macAddress = "-";
524 }
526 $this->netConfigDNS->cn = $this->cn;
527 $this->netConfigDNS->save($this->dn);
528 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic with dn '%s' failed."),$this->dn));
529 }
532 /* Display generic part for server copy & paste */
533 function getCopyDialog()
534 {
535 $vars = array("cn");
536 $smarty = get_smarty();
537 $smarty->assign("cn" ,$this->cn);
538 $smarty->assign("object","workstation");
539 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
540 $ret = array();
541 $ret['string'] = $str;
542 $ret['status'] = "";
543 return($ret);
544 }
547 function saveCopyDialog()
548 {
549 if(isset($_POST['cn'])){
550 $this->cn = $_POST['cn'];
551 }
552 }
555 function PrepareForCopyPaste($source)
556 {
557 plugin::PrepareForCopyPaste($source);
558 if(isset($source['macAddress'][0])){
559 $this->netConfigDNS->macAddress = $source['macAddress'][0];
560 }
561 if(isset($source['ipHostNumber'][0])){
562 $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
563 }
565 /* Create used ntp server array */
566 $this->gotoNtpServer= array();
567 if(isset($source['gotoNtpServer'])){
568 $this->inheritTimeServer = false;
569 unset($source['gotoNtpServer']['count']);
570 foreach($source['gotoNtpServer'] as $server){
571 $this->gotoNtpServer[$server] = $server;
572 }
573 }
575 /* Set inherit checkbox state */
576 if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer)==0)){
577 $this->inheritTimeServer = true;
578 $this->gotoNtpServer=array();
579 }
580 }
583 /* Return plugin informations for acl handling
584 #FIXME FAIscript seams to ununsed within this class... */
585 function plInfo()
586 {
587 return (array(
588 "plShortName" => _("Generic"),
589 "plDescription" => _("Workstation generic"),
590 "plSelfModify" => FALSE,
591 "plDepends" => array(),
592 "plPriority" => 0,
593 "plSection" => array("administration"),
594 "plCategory" => array("workstation" => array("description" => _("Workstation"),
595 "objectClass" => "gotoWorkstation")),
596 "plProvidedAcls"=> array(
597 "cn" => _("Workstation name"),
598 "l" => _("Location") ,
599 "base" => _("Base") ,
600 "gotoMode" => _("Goto mode"),
601 "gotoSyslogServer" => _("Syslog server"),
602 "gotoNtpServer" => _("Ntp server"),
603 "gotoRootPasswd" => _("Root password"),
604 "FAIstate" => _("Action flag"))
605 ));
606 }
608 }
610 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
611 ?>