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" => "localboot",
55 "localboot" => "localboot",
56 "instant_update" => "softupdate",
57 "update" => "sceduledupdate",
58 "reinstall" => "install",
59 "rescan" => "",
60 "memcheck" => "memcheck",
61 "sysinfo" => "sysinfo");
64 var $fai_activated = FALSE;
66 function workgeneric ($config, $dn= NULL, $parent= NULL)
67 {
68 $tmp = search_config($config->data,"faiManagement","CLASS");
69 if(!empty($tmp)){
70 $this->fai_activated = TRUE;
71 }
73 plugin::plugin ($config, $dn, $parent);
74 $this->netConfigDNS = new termDNS($this->config,$this->dn,$this->objectclasses);
76 /* Read arrays */
77 foreach (array("ghNetNic", "ghIdeDev", "ghScsiDev") as $val){
78 if (!isset($this->attrs[$val])){
79 continue;
80 }
81 for ($i= 0; $i<$this->attrs[$val]['count']; $i++){
82 array_push($this->$val, $this->attrs[$val][$i]);
83 }
84 }
86 /* Create used ntp server array */
87 $this->gotoNtpServer= array();
88 if(isset($this->attrs['gotoNtpServer'])){
89 $this->inheritTimeServer = false;
90 unset($this->attrs['gotoNtpServer']['count']);
91 foreach($this->attrs['gotoNtpServer'] as $server){
92 $this->gotoNtpServer[$server] = $server;
93 }
94 }
96 /* Set inherit checkbox state */
97 if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer) == 0)){
98 $this->inheritTimeServer = true;
99 $this->gotoNtpServer=array();
100 }
102 /* Create available ntp options */
103 $tmp = $this->config->data['SERVERS']['NTP'];
104 $this->gotoNtpServers = array();
105 foreach($tmp as $key => $server){
106 if($server == "default") continue;
107 $this->gotoNtpServers[$server] = $server;
108 }
110 $this->modes["active"]= _("Activated");
111 $this->modes["locked"]= _("Locked");
113 /* Set base */
114 if ($this->dn == "new"){
115 $ui= get_userinfo();
116 $this->base= dn2base($ui->dn);
117 } else {
118 $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
119 }
121 /* Create an array of all Syslog servers */
122 $tmp = $this->config->data['SERVERS']['SYSLOG'];
123 foreach($tmp as $server){
124 $visible = $server;
125 if($server == "default") {
126 $visible = "["._("inherited")."]";
127 }
128 $this->gotoSyslogServers[$server] = $visible;
129 }
131 /* Save 'dn' for later referal */
132 $this->orig_dn= $this->dn;
133 }
136 function set_acl_base($base)
137 {
138 plugin::set_acl_base($base);
139 $this->netConfigDNS->set_acl_base($base);
140 }
142 function set_acl_category($cat)
143 {
144 plugin::set_acl_category($cat);
145 $this->netConfigDNS->set_acl_category($cat);
146 }
148 function execute()
149 {
150 /* Call parent execute */
151 plugin::execute();
153 if($this->is_account && !$this->view_logged){
154 $this->view_logged = TRUE;
155 @log::log("view","workstation/".get_class($this),$this->dn);
156 }
158 /* Do we need to flip is_account state? */
159 if(isset($_POST['modify_state'])){
160 if($this->is_account && $this->acl_is_removeable()){
161 $this->is_account= FALSE;
162 }elseif(!$this->is_account && $this->acl_is_createable()){
163 $this->is_account= TRUE;
164 }
165 }
167 if ((isset($_POST['action'])) && ($this->acl_is_writeable("FAIstate"))){
168 $cmd= search_config($this->config->data['TABS'], "workgeneric", "ACTIONCMD");
169 if ($cmd == ""){
170 print_red(_("No ACTIONCMD definition found in your gosa.conf"));
171 } else {
172 exec ($cmd." ".$this->netConfigDNS->macAddress." ".escapeshellarg($_POST['saction']), $dummy, $retval);
173 if ($retval != 0){
174 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
175 } elseif ($_POST['saction'] != "wake") {
177 /* Set FAIstate */
178 if($this->fai_activated && $this->dn != "new"){
179 $ldap = $this->config->get_ldap_link();
180 $ldap->cd($this->config->current['BASE']);
181 $ldap->cat($this->dn,array("objectClass"));
182 $res = $ldap->fetch();
184 $attrs = array();
185 $attrs['FAIstate'] = "";
186 if(isset($this->mapActions[$_POST['saction']])){
187 $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
188 }
190 for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
191 $attrs['objectClass'][] = $res['objectClass'][$i];
192 }
194 if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
195 $attrs['objectClass'][] = "FAIobject";
196 }
198 if($attrs['FAIstate'] == ""){
199 #FIXME we should check if FAIobject is not used anymore
200 $attrs['FAIstate'] = array();
201 }
203 $ldap->cd($this->dn);
204 $ldap->modify($attrs);
205 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic (FAIstate) with dn '%s' failed."),$this->dn));
207 }
208 $this->didAction= TRUE;
209 }
210 }
211 }
213 /* Do we represent a valid terminal? */
214 if (!$this->is_account && $this->parent == NULL){
215 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
216 _("This 'dn' has no workstation features.")."</b>";
217 return($display);
218 }
220 /* Base select dialog */
221 $once = true;
222 foreach($_POST as $name => $value){
223 if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_writeable("base")){
224 $once = false;
225 $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
226 $this->dialog->setCurrentBase($this->base);
227 }
228 }
230 /* Dialog handling */
231 if(is_object($this->dialog)){
232 /* Must be called before save_object */
233 $this->dialog->save_object();
235 if($this->dialog->isClosed()){
236 $this->dialog = false;
237 }elseif($this->dialog->isSelected()){
239 /* A new base was selected, check if it is a valid one */
240 $tmp = $this->get_allowed_bases();
241 if(isset($tmp[$this->dialog->isSelected()])){
242 $this->base = $this->dialog->isSelected();
243 }
245 $this->dialog= false;
246 }else{
247 return($this->dialog->execute());
248 }
249 }
251 /* Add new ntp Server to our list */
252 if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers'])) && $this->acl_is_writeable("gotoNtpServer")){
253 $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
254 }
256 /* Delete selected NtpServer for list of used servers */
257 if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected'])) && $this->acl_is_writeable("gotoNtpServer")){
258 foreach($_POST['gotoNtpServerSelected'] as $name){
259 unset($this->gotoNtpServer[$name]);
260 }
261 }
263 /* Fill templating stuff */
264 $smarty= get_smarty();
266 /* Set acls */
267 $tmp = $this->plInfo();
268 foreach($tmp['plProvidedAcls'] as $name => $translation){
269 $smarty->assign($name."ACL",$this->getacl($name));
270 }
272 $smarty->assign("cn", $this->cn);
273 $smarty->assign("l", $this->l);
274 $smarty->assign("bases", $this->get_allowed_bases());
275 $smarty->assign("staticAddress", "");
277 $tmp = array();
278 foreach($this->gotoNtpServers as $server){
279 if(!in_array($server,$this->gotoNtpServer)){
280 $tmp[$server] = $server;
281 }
282 }
283 $smarty->assign("gotoNtpServers",$tmp);
285 /* Check if workstation is online */
286 $query= "fping -q -r 1 -t 500 ".$this->cn;
287 exec ($query, $dummy, $retval);
289 /* Offline */
290 if ($retval == 0){
291 $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
292 "instant_update" => _("Instant update"),
293 "update" => _("Scheduled update"),
294 "reinstall" => _("Reinstall"),
295 "rescan" => _("Rescan hardware"),
296 "memcheck" => _("Memory test"),
297 "localboot" => _("Force localboot"),
298 "sysinfo" => _("System analysis")));
299 } else {
300 $smarty->assign("actions", array("wake" => _("Wake up"),
301 "reinstall" => _("Reinstall"),
302 "update" => _("Scheduled update"),
303 "memcheck" => _("Memory test"),
304 "localboot" => _("Force localboot"),
305 "sysinfo" => _("System analysis")));
306 }
307 /* Arrays */
308 $smarty->assign("modes", $this->modes);
309 $smarty->assign("nfsservers", $this->config->data['SERVERS']['NFS']);
310 $smarty->assign("syslogservers", $this->gotoSyslogServers);
311 $smarty->assign("fai_activated",$this->fai_activated);
313 $ntpser = array();
314 foreach($this->gotoNtpServers as $server){
315 if(!in_array($server,$this->gotoNtpServer)){
316 $ntpser[$server] = $server;
317 }
318 }
319 $smarty->assign("gotoNtpServers", $ntpser);
321 /* Variables */
322 foreach(array("base", "gotoMode", "gotoSyslogServer", "gotoNtpServer") as $val){
323 $smarty->assign($val."_select", $this->$val);
324 }
326 /* tell smarty the inherit checkbox state */
327 $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
329 /* Show main page */
330 $smarty->assign("netconfig", $this->netConfigDNS->execute());
331 return($smarty->fetch (get_template_path('workstation.tpl', TRUE)));
332 }
334 function remove_from_parent()
335 {
336 if($this->acl_is_removeable()){
338 $this->netConfigDNS->remove_from_parent();
339 $ldap= $this->config->get_ldap_link();
340 $ldap->rmdir($this->dn);
341 @log::log("remove","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
342 show_ldap_error($ldap->get_error(), sprintf(_("Removing of system workstation/generic with dn '%s' failed."),$this->dn));
344 /* Optionally execute a command after we're done */
345 $this->handle_post_events("remove", array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS-ipHostNumber));
347 /* Delete references to object groups */
348 $ldap->cd ($this->config->current['BASE']);
349 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
350 while ($ldap->fetch()){
351 $og= new ogroup($this->config, $ldap->getDN());
352 unset($og->member[$this->dn]);
353 $og->save ();
354 }
355 }
356 }
359 /* Save data to object */
360 function save_object()
361 {
363 /* Create a base backup and reset the
364 base directly after calling plugin::save_object();
365 Base will be set seperatly a few lines below */
366 $base_tmp = $this->base;
367 plugin::save_object();
368 $this->base = $base_tmp;
370 /* Save base, since this is no LDAP attribute */
371 $tmp = $this->get_allowed_bases();
372 if(isset($_POST['base'])){
373 if(isset($tmp[$_POST['base']])){
374 $this->base= $_POST['base'];
375 }
376 }
378 $this->netConfigDNS->save_object();
380 /* Set inherit mode */
381 if((isset($_POST['workgeneric_posted'])) && ($this->acl_is_writeable("gotoNtpServer"))){
382 if(isset($_POST["inheritTimeServer"])){
383 $this->inheritTimeServer = true;
384 }else{
385 $this->inheritTimeServer = false;
386 }
387 }
389 }
392 /* Check supplied data */
393 function check()
394 {
395 /* Call common method to give check the hook */
396 $message= plugin::check();
398 /* Skip IP & Mac checks if this is a template */
399 if($this->cn != "wdefault"){
400 $message= array_merge($message, $this->netConfigDNS->check());
401 }
403 $this->dn= "cn=".$this->cn.",ou=workstations,ou=systems,".$this->base;
405 if ($this->cn == ""){
406 $message[]= _("The required field 'Workstation name' is not set.");
407 }
409 if ($this->orig_dn != $this->dn){
410 $ldap= $this->config->get_ldap_link();
411 $ldap->cd ($this->base);
413 if($this->cn == "wdefault"){
414 $ldap->cat($this->dn);
415 }else{
416 $ldap->search ("(&(cn=".$this->cn.")(objectClass=gotoWorkstation))", array("cn"));
417 }
418 if ($ldap->count() != 0){
419 while ($attrs= $ldap->fetch()){
420 if (preg_match ("/,ou=incoming,/", $ldap->getDN())){
421 continue;
422 } else {
423 if ($attrs['dn'] != $this->orig_dn){
424 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
425 break;
426 }
427 }
428 }
429 }
430 }
432 /* Check for valid ntpServer selection */
433 if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
434 $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
435 }
437 return ($message);
438 }
441 /* Save to LDAP */
442 function save()
443 {
444 plugin::save();
446 /* Strip out 'default' values */
447 foreach (array("gotoSyslogServer") as $val){
449 if (isset($this->attrs[$val]) && $this->attrs[$val] == "default"){
450 $this->attrs[$val]= array();
451 }
452 }
454 /* Add missing arrays */
455 foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
456 if (isset ($this->$val) && count ($this->$val) != 0){
457 $this->attrs["$val"]= $this->$val;
458 }
459 }
461 /* Remove all empty values */
462 if ($this->orig_dn == 'new'){
463 $attrs= array();
464 foreach ($this->attrs as $key => $val){
465 if (is_array($val) && count($val) == 0){
466 continue;
467 }
468 $attrs[$key]= $val;
469 }
470 $this->attrs= $attrs;
471 }
473 /* Update ntp server settings */
474 if($this->inheritTimeServer){
475 if($this->is_new){
476 if(isset($this->attrs['gotoNtpServer'])){
477 unset($this->attrs['gotoNtpServer']);
478 }
479 }else{
480 $this->attrs['gotoNtpServer'] = array();
481 }
482 }else{
483 /* Set ntpServers */
484 $this->attrs['gotoNtpServer'] = array();
485 foreach($this->gotoNtpServer as $server){
486 $this->attrs['gotoNtpServer'][] = $server;
487 }
488 }
490 if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
491 $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
492 }
494 /* Write back to ldap */
495 $ldap= $this->config->get_ldap_link();
496 if ($this->orig_dn == 'new'){
497 $ldap->cd($this->config->current['BASE']);
498 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
499 $ldap->cd($this->dn);
500 $ldap->add($this->attrs);
501 @log::log("create","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
502 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic with dn '%s' failed."),$this->dn));
503 if(!$this->didAction){
504 $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
505 }
506 } else {
507 if ($this->orig_dn != $this->dn){
508 $this->move($this->orig_dn, $this->dn);
509 }
510 $ldap->cd($this->dn);
511 $this->cleanup();
512 $ldap->modify ($this->attrs);
513 @log::log("modify","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
515 if(!$this->didAction){
516 $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
517 }
518 }
520 /* cn=default and macAddress=- indicates that this is a template */
521 if($this->cn == "wdefault"){
522 $this->netConfigDNS->macAddress = "-";
523 }
525 $this->netConfigDNS->cn = $this->cn;
526 $this->netConfigDNS->save($this->dn);
527 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic with dn '%s' failed."),$this->dn));
528 }
531 /* Return plugin informations for acl handling
532 #FIXME FAIscript seams to ununsed within this class... */
533 function plInfo()
534 {
535 return (array(
536 "plShortName" => _("Generic"),
537 "plDescription" => _("Workstation generic"),
538 "plSelfModify" => FALSE,
539 "plDepends" => array(),
540 "plPriority" => 0,
541 "plSection" => array("administration"),
542 "plCategory" => array("workstation" => array("description" => _("Workstation"),
543 "objectClass" => "gotoWorkstation")),
544 "plProvidedAcls"=> array(
545 "cn" => _("Workstation name"),
546 "l" => _("Location") ,
547 "base" => _("Base") ,
548 "gotoMode" => _("Goto mode"),
549 "gotoSyslogServer" => _("Syslog server"),
550 "gotoNtpServer" => _("Ntp server"),
551 "gotoRootPasswd" => _("Root password"),
552 "FAIstate" => _("Action flag"))
553 ));
554 }
556 }
558 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
559 ?>