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