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