Code

Fixed dns zone creation , if server is new
[gosa.git] / plugins / admin / systems / class_servGeneric.inc
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                             "update"          => "sceduledupdate",
34                             "reinstall"       => "install",
35                             "rescan"          => "",
36                             "memcheck"        => "memcheck",
37                             "sysinfo"         => "sysinfo");
39   var $fai_activated  =FALSE;
41   function servgeneric ($config, $dn= NULL, $parent= NULL)
42   {
43     /* Check if FAI is activated */
44     $tmp = search_config($config->data,"faiManagement","CLASS");
45     if(!empty($tmp)){
46       $this->fai_activated = TRUE;
47     }
49     plugin::plugin ($config, $dn, $parent);
51     $this->ui = get_userinfo();
52     $this->modes["active"]= _("Activated");
53     $this->modes["locked"]= _("Locked");
55     /* Set base */
56     if ($this->dn == "new"){
57       $ui= get_userinfo();
58       $this->base= dn2base($ui->dn);
59       $this->cn= "";
60     } else {
61       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
62     }
63     $this->netConfigDNS = new termDNS($this->config,$this->dn,$this->objectclasses);
64     $this->netConfigDNS->set_acl_category("server");
65     $this->netConfigDNS->set_acl_base($this->base);
66   
67     /* Save dn for later references */
68     $this->orig_dn= $this->dn;
69   }
71   function execute()
72   {
73     /* Call parent execute */
74     plugin::execute();
76     /* Do we represent a valid server? */
77     if (!$this->is_account && $this->parent == NULL){
78       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
79         _("This 'dn' has no server features.")."</b>";
80       return($display);
81     }
83     /* Check for action */
84     if (isset($_POST['action']) && $this->acl_is_writeable("FAIstate")){
85       $cmd= search_config($this->config->data['TABS'], "workgeneric", "ACTIONCMD");
86       if ($cmd == ""){
87         print_red(_("No ACTIONCMD definition found in your gosa.conf"));
88       } else {
90         exec ($cmd." ".$this->netConfigDNS->macAddress." ".escapeshellarg($_POST['saction']), $dummy, $retval);
91         if ($retval != 0){
92           print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
93         } else {
94           $this->didAction= TRUE;
96           /* Set FAIstate */
97           if($this->fai_activated && $this->dn != "new"){
98             $ldap = $this->config->get_ldap_link();
99             $ldap->cd($this->config->current['BASE']);
100             $ldap->cat($this->dn,array("objectClass"));
101             $res = $ldap->fetch();
103             $attrs = array();
104             $attrs['FAIstate'] = "";
105             if(isset($this->mapActions[$_POST['saction']])){
106               $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
107             }
109             for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
110               $attrs['objectClass'][] = $res['objectClass'][$i];
111             }
113             if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
114               $attrs['objectClass'][] = "FAIobject";
115             }
117             if($attrs['FAIstate'] == ""){
118 #FIXME we should check if FAIobject is used anymore
119               $attrs['FAIstate'] = array();
120             }
122             $ldap->cd($this->dn);
123             $ldap->modify($attrs);
124           }
125           show_ldap_error($ldap->get_error(), sprintf(_("Saving of system server/generic (FAIstate) with dn '%s' failed."),$this->dn)); 
126         }
127       }
128     }
130     /* Base select dialog */
131     $once = true;
132     foreach($_POST as $name => $value){
133       if(preg_match("/^chooseBase/",$name) && $once ){
134         $once = false;
135         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
136         $this->dialog->setCurrentBase($this->base);
137       }
138     }
140     /* Dialog handling */
141     if(is_object($this->dialog)){
142       /* Must be called before save_object */
143       $this->dialog->save_object();
145       if($this->dialog->isClosed()){
146         $this->dialog = false;
147       }elseif($this->dialog->isSelected()){
148   
149         /* Only accept allowed bases */
150         $tmp = $this->get_allowed_bases();
151         if($tmp[$this->dialog->isSelected()]){
152           $this->base = $this->dialog->isSelected();
153         }
154         $this->dialog= false;
155       }else{
156         return($this->dialog->execute());
157       }
158     }
160     /* Fill templating stuff */
161     $smarty= get_smarty();
163     $tmp = $this->plInfo();
164     foreach($tmp['plProvidedAcls'] as $name => $translated){
165       $smarty->assign($name."ACL",$this->getacl($name));
166     }
168     /* Assign base ACL */
169     $smarty->assign("bases"   , $this->get_allowed_bases());
171     /* Assign attributes */
172     foreach ($this->attributes as $attr){
173       $smarty->assign("$attr", $this->$attr);
174     }
176     $smarty->assign("staticAddress", "");
177     $smarty->assign("base_select", $this->base);
179     /* Assign status */
180     $query= "fping -q -r 1 -t 500 ".$this->cn;
181     exec ($query, $dummy, $retval);
183     /* Offline */
184     if ($retval == 0){
185       $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
186                                        "instant_update" => _("Instant update"),
187                                        "update" => _("Scheduled update"),
188                                        "reinstall" => _("Reinstall"),
189                                        "rescan" => _("Rescan hardware"),
190                                        "memcheck" => _("Memory test"),
191                                        "sysinfo"  => _("System analysis")));
192     } else {
193       $smarty->assign("actions", array("wake" => _("Wake up"),
194                                        "reinstall" => _("Reinstall"),
195                                        "update" => _("Scheduled update"),
196                                        "memcheck" => _("Memory test"),
197                                        "sysinfo"  => _("System analysis")));
198     }
200     /* Show main page */
201     $smarty->assign("fai_activated",$this->fai_activated);
202     $smarty->assign("netconfig", $this->netConfigDNS->execute());
203     $smarty->assign("modes", $this->modes);
205     return($smarty->fetch (get_template_path('server.tpl', TRUE)));
206   }
208   function remove_from_parent()
209   {
210     /* Cancel if there's nothing to do here */
211     if ((!$this->initially_was_account) || (!$this->acl_is_removeable())){
212       return;
213     }
215     $this->netConfigDNS->remove_from_parent();
216     $ldap= $this->config->get_ldap_link();
217     $ldap->rmdir($this->dn);
218     show_ldap_error($ldap->get_error(), sprintf(_("Removing of system server/generic with dn '%s' failed."),$this->dn)); 
220     /* Delete references to object groups */
221     $ldap->cd ($this->config->current['BASE']);
222     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
223     while ($ldap->fetch()){
224       $og= new ogroup($this->config, $ldap->getDN());
225       unset($og->member[$this->dn]);
226       $og->save ();
227     }
228     $this->handle_post_events("remove", array("macAddress" => $this->netConfigDNS->macAddress));
229   }
232   /* Save data to object */
233   function save_object()
234   {
235     /* Save current base, to be able to revert to last base, 
236         if new base is invalid or not allowed to be selected */
237     $base_tmp = $this->base;
238     plugin::save_object();
239     $this->netConfigDNS->save_object();
241     /* Get base selection */
242     $tmp = $this->get_allowed_bases();
243     if(isset($_POST['base'])){
244       if($tmp[$_POST['base']]){
245         $this->base = $_POST['base'];
246       }else{
247         $this->base = $base_tmp;
248       }
249     }
250   }
253   /* Check supplied data */
254   function check()
255   {
256     /* Call common method to give check the hook */
257     $message= plugin::check();
258     $message= array_merge($message, $this->netConfigDNS->check());
259     $this->dn= "cn=".$this->cn.",ou=servers,ou=systems,".$this->base;
261     /* must: cn */
262     if ($this->cn == ""){
263       $message[]= _("The required field 'Server name' is not set.");
264     }
265     
266     if ($this->orig_dn != $this->dn){
267       $ldap= $this->config->get_ldap_link();
268       $ldap->cd ($this->base);
269       $ldap->search ("(cn=".$this->cn.")", array("cn"));
270       if ($ldap->count() != 0){
271         while ($attrs= $ldap->fetch()){
272           if ($attrs['dn'] != $this->orig_dn){
273             if(!preg_match("/,ou=incoming,/",$attrs['dn'])){
274               $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
275               break;
276             }
277           }
278         }
279       }
280     }
282     return ($message);
283   }
286   /* Save to LDAP */
287   function save()
288   {
289     plugin::save();
291     /* Remove all empty values */
292     if ($this->orig_dn == 'new'){
293       $attrs= array();
294       foreach ($this->attrs as $key => $val){
295         if (is_array($val) && count($val) == 0){
296           continue;
297         }
298         $attrs[$key]= $val;
299       }
300       $this->attrs= $attrs;
301     }
303     /* Write back to ldap */
304     $ldap= $this->config->get_ldap_link();
305     if ($this->orig_dn == 'new'){
306       $ldap->cd($this->config->current['BASE']);
307       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
308       $ldap->cd($this->dn);
309       $ldap->add($this->attrs);
310       $mode= "add";
311     } else {
312    
313       /* cn is not case sensitive for ldap, but for php it is!! */ 
314       if($this->config->current['DNMODE'] == "cn"){
315         if (strtolower($this->orig_dn) != (strtolower($this->dn))){
316           $this->move($this->orig_dn, $this->dn);
317           plugin::save();
318         }
319       }else{
320         if ($this->orig_dn != $this->dn){
321           $this->move($this->orig_dn, $this->dn);
322           plugin::save();
323         }
324       }
325   
326       $ldap->cd($this->dn);
327       $this->cleanup();
328       $ldap->modify ($this->attrs); 
330       $mode= "modify";
331     }
332     show_ldap_error($ldap->get_error(), sprintf(_("Saving of system server/generic with dn '%s' failed."),$this->dn)); 
333     $this->netConfigDNS->cn = $this->cn;
334     $this->netConfigDNS->save($this->dn);
336     /* Optionally execute a command after we're done */
337     if(!$this->didAction){
338       $this->handle_post_events($mode);
339     }
340   }
343   /* Return plugin informations for acl handling */ 
344   function plInfo()
345   {
346     return (array( 
347           "plShortName"   => _("Generic"),
348           "plDescription" => _("Server generic"),
349           "plSelfModify"  => FALSE,
350           "plDepends"     => array(),
351           "plPriority"    => 1,
352           "plSection"     => array("administration"),           
353           "plCategory"    => array("server" => array("description"  => _("Server"),
354                                                      "objectClass"  => "gotoWorkstation")),
355           "plProvidedAcls"=> array(
356             "cn"           => _("Name"),
357             "description"  => _("Description"),
358             "gotoMode"     => _("Goto mode"),
359             "base"         => _("Base"),
360             "FAIstate"     => _("Action flag"))
361           ));
362   }
365 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
366 ?>