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 if($this->is_account){
131 @log::log("view","workstation/".get_class($this),$this->dn);
132 }
134 /* Save 'dn' for later referal */
135 $this->orig_dn= $this->dn;
136 }
139 function set_acl_base($base)
140 {
141 plugin::set_acl_base($base);
142 $this->netConfigDNS->set_acl_base($base);
143 }
145 function set_acl_category($cat)
146 {
147 plugin::set_acl_category($cat);
148 $this->netConfigDNS->set_acl_category($cat);
149 }
151 function execute()
152 {
153 /* Call parent execute */
154 plugin::execute();
156 /* Do we need to flip is_account state? */
157 if(isset($_POST['modify_state'])){
158 if($this->is_account && $this->acl_is_removeable()){
159 $this->is_account= FALSE;
160 }elseif(!$this->is_account && $this->acl_is_createable()){
161 $this->is_account= TRUE;
162 }
163 }
165 if ((isset($_POST['action'])) && ($this->acl_is_writeable("FAIstate"))){
166 $cmd= search_config($this->config->data['TABS'], "workgeneric", "ACTIONCMD");
167 if ($cmd == ""){
168 print_red(_("No ACTIONCMD definition found in your gosa.conf"));
169 } else {
170 exec ($cmd." ".$this->netConfigDNS->macAddress." ".escapeshellarg($_POST['saction']), $dummy, $retval);
171 if ($retval != 0){
172 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
173 } elseif ($_POST['saction'] != "wake") {
175 /* Set FAIstate */
176 if($this->fai_activated && $this->dn != "new"){
177 $ldap = $this->config->get_ldap_link();
178 $ldap->cd($this->config->current['BASE']);
179 $ldap->cat($this->dn,array("objectClass"));
180 $res = $ldap->fetch();
182 $attrs = array();
183 $attrs['FAIstate'] = "";
184 if(isset($this->mapActions[$_POST['saction']])){
185 $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
186 }
188 for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
189 $attrs['objectClass'][] = $res['objectClass'][$i];
190 }
192 if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
193 $attrs['objectClass'][] = "FAIobject";
194 }
196 if($attrs['FAIstate'] == ""){
197 #FIXME we should check if FAIobject is not used anymore
198 $attrs['FAIstate'] = array();
199 }
201 $ldap->cd($this->dn);
202 $ldap->modify($attrs);
203 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic (FAIstate) with dn '%s' failed."),$this->dn));
205 }
206 $this->didAction= TRUE;
207 }
208 }
209 }
211 /* Do we represent a valid terminal? */
212 if (!$this->is_account && $this->parent == NULL){
213 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
214 _("This 'dn' has no workstation features.")."</b>";
215 return($display);
216 }
218 /* Base select dialog */
219 $once = true;
220 foreach($_POST as $name => $value){
221 if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_writeable("base")){
222 $once = false;
223 $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
224 $this->dialog->setCurrentBase($this->base);
225 }
226 }
228 /* Dialog handling */
229 if(is_object($this->dialog)){
230 /* Must be called before save_object */
231 $this->dialog->save_object();
233 if($this->dialog->isClosed()){
234 $this->dialog = false;
235 }elseif($this->dialog->isSelected()){
237 /* A new base was selected, check if it is a valid one */
238 $tmp = $this->get_allowed_bases();
239 if(isset($tmp[$this->dialog->isSelected()])){
240 $this->base = $this->dialog->isSelected();
241 }
243 $this->dialog= false;
244 }else{
245 return($this->dialog->execute());
246 }
247 }
249 /* Add new ntp Server to our list */
250 if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers'])) && $this->acl_is_writeable("gotoNtpServer")){
251 $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
252 }
254 /* Delete selected NtpServer for list of used servers */
255 if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected'])) && $this->acl_is_writeable("gotoNtpServer")){
256 foreach($_POST['gotoNtpServerSelected'] as $name){
257 unset($this->gotoNtpServer[$name]);
258 }
259 }
261 /* Fill templating stuff */
262 $smarty= get_smarty();
264 /* Set acls */
265 $tmp = $this->plInfo();
266 foreach($tmp['plProvidedAcls'] as $name => $translation){
267 $smarty->assign($name."ACL",$this->getacl($name));
268 }
270 $smarty->assign("cn", $this->cn);
271 $smarty->assign("l", $this->l);
272 $smarty->assign("bases", $this->get_allowed_bases());
273 $smarty->assign("staticAddress", "");
275 $tmp = array();
276 foreach($this->gotoNtpServers as $server){
277 if(!in_array($server,$this->gotoNtpServer)){
278 $tmp[$server] = $server;
279 }
280 }
281 $smarty->assign("gotoNtpServers",$tmp);
283 /* Check if workstation is online */
284 $query= "fping -q -r 1 -t 500 ".$this->cn;
285 exec ($query, $dummy, $retval);
287 /* Offline */
288 if ($retval == 0){
289 $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
290 "instant_update" => _("Instant update"),
291 "update" => _("Scheduled update"),
292 "reinstall" => _("Reinstall"),
293 "rescan" => _("Rescan hardware"),
294 "memcheck" => _("Memory test"),
295 "localboot" => _("Force localboot"),
296 "sysinfo" => _("System analysis")));
297 } else {
298 $smarty->assign("actions", array("wake" => _("Wake up"),
299 "reinstall" => _("Reinstall"),
300 "update" => _("Scheduled update"),
301 "memcheck" => _("Memory test"),
302 "localboot" => _("Force localboot"),
303 "sysinfo" => _("System analysis")));
304 }
305 /* Arrays */
306 $smarty->assign("modes", $this->modes);
307 $smarty->assign("nfsservers", $this->config->data['SERVERS']['NFS']);
308 $smarty->assign("syslogservers", $this->gotoSyslogServers);
309 $smarty->assign("fai_activated",$this->fai_activated);
311 $ntpser = array();
312 foreach($this->gotoNtpServers as $server){
313 if(!in_array($server,$this->gotoNtpServer)){
314 $ntpser[$server] = $server;
315 }
316 }
317 $smarty->assign("gotoNtpServers", $ntpser);
319 /* Variables */
320 foreach(array("base", "gotoMode", "gotoSyslogServer", "gotoNtpServer") as $val){
321 $smarty->assign($val."_select", $this->$val);
322 }
324 /* tell smarty the inherit checkbox state */
325 $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
327 /* Show main page */
328 $smarty->assign("netconfig", $this->netConfigDNS->execute());
329 return($smarty->fetch (get_template_path('workstation.tpl', TRUE)));
330 }
332 function remove_from_parent()
333 {
334 if($this->acl_is_removeable()){
336 $this->netConfigDNS->remove_from_parent();
337 $ldap= $this->config->get_ldap_link();
338 $ldap->rmdir($this->dn);
339 @log::log("remove","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
340 show_ldap_error($ldap->get_error(), sprintf(_("Removing of system workstation/generic with dn '%s' failed."),$this->dn));
342 /* Optionally execute a command after we're done */
343 $this->handle_post_events("remove", array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS-ipHostNumber));
345 /* Delete references to object groups */
346 $ldap->cd ($this->config->current['BASE']);
347 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
348 while ($ldap->fetch()){
349 $og= new ogroup($this->config, $ldap->getDN());
350 unset($og->member[$this->dn]);
351 $og->save ();
352 }
353 }
354 }
357 /* Save data to object */
358 function save_object()
359 {
361 /* Create a base backup and reset the
362 base directly after calling plugin::save_object();
363 Base will be set seperatly a few lines below */
364 $base_tmp = $this->base;
365 plugin::save_object();
366 $this->base = $base_tmp;
368 /* Save base, since this is no LDAP attribute */
369 $tmp = $this->get_allowed_bases();
370 if(isset($_POST['base'])){
371 if(isset($tmp[$_POST['base']])){
372 $this->base= $_POST['base'];
373 }
374 }
376 $this->netConfigDNS->save_object();
378 /* Set inherit mode */
379 if((isset($_POST['workgeneric_posted'])) && ($this->acl_is_writeable("gotoNtpServer"))){
380 if(isset($_POST["inheritTimeServer"])){
381 $this->inheritTimeServer = true;
382 }else{
383 $this->inheritTimeServer = false;
384 }
385 }
387 }
390 /* Check supplied data */
391 function check()
392 {
393 /* Call common method to give check the hook */
394 $message= plugin::check();
396 /* Skip IP & Mac checks if this is a template */
397 if($this->cn != "wdefault"){
398 $message= array_merge($message, $this->netConfigDNS->check());
399 }
401 $this->dn= "cn=".$this->cn.",ou=workstations,ou=systems,".$this->base;
403 if ($this->cn == ""){
404 $message[]= _("The required field 'Workstation name' is not set.");
405 }
407 if ($this->orig_dn != $this->dn){
408 $ldap= $this->config->get_ldap_link();
409 $ldap->cd ($this->base);
411 if($this->cn == "wdefault"){
412 $ldap->cat($this->dn);
413 }else{
414 $ldap->search ("(&(cn=".$this->cn.")(objectClass=gotoWorkstation))", array("cn"));
415 }
416 if ($ldap->count() != 0){
417 while ($attrs= $ldap->fetch()){
418 if (preg_match ("/,ou=incoming,/", $ldap->getDN())){
419 continue;
420 } else {
421 if ($attrs['dn'] != $this->orig_dn){
422 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
423 break;
424 }
425 }
426 }
427 }
428 }
430 /* Check for valid ntpServer selection */
431 if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
432 $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
433 }
435 return ($message);
436 }
439 /* Save to LDAP */
440 function save()
441 {
442 plugin::save();
444 /* Strip out 'default' values */
445 foreach (array("gotoSyslogServer") as $val){
447 if (isset($this->attrs[$val]) && $this->attrs[$val] == "default"){
448 $this->attrs[$val]= array();
449 }
450 }
452 /* Add missing arrays */
453 foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
454 if (isset ($this->$val) && count ($this->$val) != 0){
455 $this->attrs["$val"]= $this->$val;
456 }
457 }
459 /* Remove all empty values */
460 if ($this->orig_dn == 'new'){
461 $attrs= array();
462 foreach ($this->attrs as $key => $val){
463 if (is_array($val) && count($val) == 0){
464 continue;
465 }
466 $attrs[$key]= $val;
467 }
468 $this->attrs= $attrs;
469 }
471 /* Update ntp server settings */
472 if($this->inheritTimeServer){
473 if($this->is_new){
474 if(isset($this->attrs['gotoNtpServer'])){
475 unset($this->attrs['gotoNtpServer']);
476 }
477 }else{
478 $this->attrs['gotoNtpServer'] = array();
479 }
480 }else{
481 /* Set ntpServers */
482 $this->attrs['gotoNtpServer'] = array();
483 foreach($this->gotoNtpServer as $server){
484 $this->attrs['gotoNtpServer'][] = $server;
485 }
486 }
488 if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
489 $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
490 }
492 /* Write back to ldap */
493 $ldap= $this->config->get_ldap_link();
494 if ($this->orig_dn == 'new'){
495 $ldap->cd($this->config->current['BASE']);
496 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
497 $ldap->cd($this->dn);
498 $ldap->add($this->attrs);
499 @log::log("create","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
500 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic with dn '%s' failed."),$this->dn));
501 if(!$this->didAction){
502 $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
503 }
504 } else {
505 if ($this->orig_dn != $this->dn){
506 $this->move($this->orig_dn, $this->dn);
507 }
508 $ldap->cd($this->dn);
509 $this->cleanup();
510 $ldap->modify ($this->attrs);
511 @log::log("modify","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
513 if(!$this->didAction){
514 $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
515 }
516 }
518 /* cn=default and macAddress=- indicates that this is a template */
519 if($this->cn == "wdefault"){
520 $this->netConfigDNS->macAddress = "-";
521 }
523 $this->netConfigDNS->cn = $this->cn;
524 $this->netConfigDNS->save($this->dn);
525 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic with dn '%s' failed."),$this->dn));
526 }
529 /* Return plugin informations for acl handling
530 #FIXME FAIscript seams to ununsed within this class... */
531 function plInfo()
532 {
533 return (array(
534 "plShortName" => _("Generic"),
535 "plDescription" => _("Workstation generic"),
536 "plSelfModify" => FALSE,
537 "plDepends" => array(),
538 "plPriority" => 0,
539 "plSection" => array("administration"),
540 "plCategory" => array("workstation" => array("description" => _("Workstation"),
541 "objectClass" => "gotoWorkstation")),
542 "plProvidedAcls"=> array(
543 "cn" => _("Workstation name"),
544 "l" => _("Location") ,
545 "base" => _("Base") ,
546 "gotoMode" => _("Goto mode"),
547 "gotoSyslogServer" => _("Syslog server"),
548 "gotoNtpServer" => _("Ntp server"),
549 "gotoRootPasswd" => _("Root password"),
550 "FAIstate" => _("Action flag"))
551 ));
552 }
554 }
556 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
557 ?>