1 <?php
3 class servgeneric extends plugin
4 {
5 /* CLI vars */
6 var $cli_summary= "Manage server 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 $ignore_account= TRUE;
12 var $interfaces= array();
14 /* Needed values and lists */
15 var $base= "";
16 var $cn= "";
17 var $l= "";
18 var $description= "";
19 var $orig_dn= "";
20 var $didAction= FALSE;
22 /* attribute list for save action */
23 var $attributes= array("cn", "description","gotoMode");
24 var $objectclasses= array("top", "GOhard", "goServer");
26 var $gotoMode= "locked";
28 var $netConfigDNS;
29 var $modes = array();
30 var $ui ;
31 var $mapActions = array("reboot" => "",
32 "instant_update" => "softupdate",
33 "localboot" => "localboot",
34 "update" => "sceduledupdate",
35 "reinstall" => "install",
36 "rescan" => "",
37 "memcheck" => "memcheck",
38 "sysinfo" => "sysinfo");
40 var $fai_activated =FALSE;
42 function servgeneric ($config, $dn= NULL, $parent= NULL)
43 {
44 /* Check if FAI is activated */
45 $tmp = search_config($config->data,"faiManagement","CLASS");
46 if(!empty($tmp)){
47 $this->fai_activated = TRUE;
48 }
50 plugin::plugin ($config, $dn, $parent);
52 $this->ui = get_userinfo();
53 $this->modes["active"]= _("Activated");
54 $this->modes["locked"]= _("Locked");
56 /* Set base */
57 if ($this->dn == "new"){
58 $ui= get_userinfo();
59 $this->base= dn2base($ui->dn);
60 $this->cn= "";
61 } else {
62 $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
63 }
64 $this->netConfigDNS = new termDNS($this->config,$this->dn,$this->objectclasses);
65 $this->netConfigDNS->set_acl_category("server");
66 $this->netConfigDNS->set_acl_base($this->base);
68 /* Save dn for later references */
69 $this->orig_dn= $this->dn;
70 }
73 function set_acl_base($base)
74 {
75 plugin::set_acl_base($base);
76 $this->netConfigDNS->set_acl_base($base);
77 }
80 function set_acl_category($cat)
81 {
82 plugin::set_acl_category($cat);
83 $this->netConfigDNS->set_acl_category($cat);
84 }
87 function execute()
88 {
89 /* Call parent execute */
90 plugin::execute();
92 /* Do we represent a valid server? */
93 if (!$this->is_account && $this->parent == NULL){
94 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
95 _("This 'dn' has no server features.")."</b>";
96 return($display);
97 }
99 /* Check for action */
100 if (isset($_POST['action']) && $this->acl_is_writeable("FAIstate")){
101 $cmd= search_config($this->config->data['TABS'], "workgeneric", "ACTIONCMD");
102 if ($cmd == ""){
103 print_red(_("No ACTIONCMD definition found in your gosa.conf"));
104 } else {
106 exec ($cmd." ".$this->netConfigDNS->macAddress." ".escapeshellarg($_POST['saction']), $dummy, $retval);
107 if ($retval != 0){
108 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
109 } else {
110 $this->didAction= TRUE;
112 /* Set FAIstate */
113 if($this->fai_activated && $this->dn != "new"){
114 $ldap = $this->config->get_ldap_link();
115 $ldap->cd($this->config->current['BASE']);
116 $ldap->cat($this->dn,array("objectClass"));
117 $res = $ldap->fetch();
119 $attrs = array();
120 $attrs['FAIstate'] = "";
121 if(isset($this->mapActions[$_POST['saction']])){
122 $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
123 }
125 for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
126 $attrs['objectClass'][] = $res['objectClass'][$i];
127 }
129 if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
130 $attrs['objectClass'][] = "FAIobject";
131 }
133 if($attrs['FAIstate'] == ""){
134 #FIXME we should check if FAIobject is used anymore
135 $attrs['FAIstate'] = array();
136 }
138 $ldap->cd($this->dn);
139 $ldap->modify($attrs);
140 }
141 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system server/generic (FAIstate) with dn '%s' failed."),$this->dn));
142 }
143 }
144 }
146 /* Base select dialog */
147 $once = true;
148 foreach($_POST as $name => $value){
149 if(preg_match("/^chooseBase/",$name) && $once ){
150 $once = false;
151 $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
152 $this->dialog->setCurrentBase($this->base);
153 }
154 }
156 /* Dialog handling */
157 if(is_object($this->dialog)){
158 /* Must be called before save_object */
159 $this->dialog->save_object();
161 if($this->dialog->isClosed()){
162 $this->dialog = false;
163 }elseif($this->dialog->isSelected()){
165 /* Only accept allowed bases */
166 $tmp = $this->get_allowed_bases();
167 if($tmp[$this->dialog->isSelected()]){
168 $this->base = $this->dialog->isSelected();
169 }
170 $this->dialog= false;
171 }else{
172 return($this->dialog->execute());
173 }
174 }
176 /* Fill templating stuff */
177 $smarty= get_smarty();
179 $tmp = $this->plInfo();
180 foreach($tmp['plProvidedAcls'] as $name => $translated){
181 $smarty->assign($name."ACL",$this->getacl($name));
182 }
184 /* Assign base ACL */
185 $smarty->assign("bases" , $this->get_allowed_bases());
187 /* Assign attributes */
188 foreach ($this->attributes as $attr){
189 $smarty->assign("$attr", $this->$attr);
190 }
192 $smarty->assign("staticAddress", "");
193 $smarty->assign("base_select", $this->base);
195 /* Assign status */
196 $query= "fping -q -r 1 -t 500 ".$this->cn;
197 exec ($query, $dummy, $retval);
199 /* Offline */
200 if ($retval == 0){
201 $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
202 "instant_update" => _("Instant update"),
203 "update" => _("Scheduled update"),
204 "reinstall" => _("Reinstall"),
205 "rescan" => _("Rescan hardware"),
206 "memcheck" => _("Memory test"),
207 "localboot" => _("Force localboot"),
208 "sysinfo" => _("System analysis")));
209 } else {
210 $smarty->assign("actions", array("wake" => _("Wake up"),
211 "reinstall" => _("Reinstall"),
212 "update" => _("Scheduled update"),
213 "memcheck" => _("Memory test"),
214 "localboot" => _("Force localboot"),
215 "sysinfo" => _("System analysis")));
216 }
218 /* Show main page */
219 $smarty->assign("fai_activated",$this->fai_activated);
220 $smarty->assign("netconfig", $this->netConfigDNS->execute());
221 $smarty->assign("modes", $this->modes);
223 return($smarty->fetch (get_template_path('server.tpl', TRUE)));
224 }
226 function remove_from_parent()
227 {
228 /* Cancel if there's nothing to do here */
229 if ((!$this->initially_was_account) || (!$this->acl_is_removeable())){
230 return;
231 }
233 $this->netConfigDNS->remove_from_parent();
234 $ldap= $this->config->get_ldap_link();
235 $ldap->rmdir($this->dn);
236 show_ldap_error($ldap->get_error(), sprintf(_("Removing of system server/generic with dn '%s' failed."),$this->dn));
238 /* Delete references to object groups */
239 $ldap->cd ($this->config->current['BASE']);
240 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
241 while ($ldap->fetch()){
242 $og= new ogroup($this->config, $ldap->getDN());
243 unset($og->member[$this->dn]);
244 $og->save ();
245 }
246 $this->handle_post_events("remove", array("macAddress" => $this->netConfigDNS->macAddress));
247 }
250 /* Save data to object */
251 function save_object()
252 {
253 /* Save current base, to be able to revert to last base,
254 if new base is invalid or not allowed to be selected */
255 $base_tmp = $this->base;
256 plugin::save_object();
257 $this->netConfigDNS->save_object();
259 /* Get base selection */
260 $tmp = $this->get_allowed_bases();
261 if(isset($_POST['base'])){
262 if($tmp[$_POST['base']]){
263 $this->base = $_POST['base'];
264 }else{
265 $this->base = $base_tmp;
266 }
267 }
268 }
271 /* Check supplied data */
272 function check()
273 {
274 /* Call common method to give check the hook */
275 $message= plugin::check();
276 $message= array_merge($message, $this->netConfigDNS->check());
277 $this->dn= "cn=".$this->cn.",ou=servers,ou=systems,".$this->base;
279 /* must: cn */
280 if ($this->cn == ""){
281 $message[]= _("The required field 'Server name' is not set.");
282 }
284 if ($this->orig_dn != $this->dn){
285 $ldap= $this->config->get_ldap_link();
286 $ldap->cd ($this->base);
287 $ldap->search ("(cn=".$this->cn.")", array("cn"));
288 if ($ldap->count() != 0){
289 while ($attrs= $ldap->fetch()){
290 if ($attrs['dn'] != $this->orig_dn){
291 if(!preg_match("/,ou=incoming,/",$attrs['dn'])){
292 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
293 break;
294 }
295 }
296 }
297 }
298 }
300 return ($message);
301 }
304 /* Save to LDAP */
305 function save()
306 {
307 plugin::save();
309 /* Remove all empty values */
310 if ($this->orig_dn == 'new'){
311 $attrs= array();
312 foreach ($this->attrs as $key => $val){
313 if (is_array($val) && count($val) == 0){
314 continue;
315 }
316 $attrs[$key]= $val;
317 }
318 $this->attrs= $attrs;
319 }
321 /* Write back to ldap */
322 $ldap= $this->config->get_ldap_link();
323 if ($this->orig_dn == 'new'){
324 $ldap->cd($this->config->current['BASE']);
325 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
326 $ldap->cd($this->dn);
327 $ldap->add($this->attrs);
328 $mode= "add";
329 } else {
331 /* cn is not case sensitive for ldap, but for php it is!! */
332 if($this->config->current['DNMODE'] == "cn"){
333 if (strtolower($this->orig_dn) != (strtolower($this->dn))){
334 $this->move($this->orig_dn, $this->dn);
335 plugin::save();
336 }
337 }else{
338 if ($this->orig_dn != $this->dn){
339 $this->move($this->orig_dn, $this->dn);
340 plugin::save();
341 }
342 }
344 $ldap->cd($this->dn);
345 $this->cleanup();
346 $ldap->modify ($this->attrs);
348 $mode= "modify";
349 }
350 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system server/generic with dn '%s' failed."),$this->dn));
351 $this->netConfigDNS->cn = $this->cn;
352 $this->netConfigDNS->save($this->dn);
354 /* Optionally execute a command after we're done */
355 if(!$this->didAction){
356 $this->handle_post_events($mode);
357 }
358 }
361 /* Return plugin informations for acl handling */
362 function plInfo()
363 {
364 return (array(
365 "plShortName" => _("Generic"),
366 "plDescription" => _("Server generic"),
367 "plSelfModify" => FALSE,
368 "plDepends" => array(),
369 "plPriority" => 1,
370 "plSection" => array("administration"),
371 "plCategory" => array("server" => array("description" => _("Server"),
372 "objectClass" => "gotoWorkstation")),
373 "plProvidedAcls"=> array(
374 "cn" => _("Name"),
375 "description" => _("Description"),
376 "gotoMode" => _("Goto mode"),
377 "base" => _("Base"),
378 "FAIstate" => _("Action flag"))
379 ));
380 }
381 }
383 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
384 ?>