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 if($this->is_account){
69 @log::log("view","server/".get_class($this),$this->dn);
70 }
72 /* Save dn for later references */
73 $this->orig_dn= $this->dn;
74 }
77 function set_acl_base($base)
78 {
79 plugin::set_acl_base($base);
80 $this->netConfigDNS->set_acl_base($base);
81 }
84 function set_acl_category($cat)
85 {
86 plugin::set_acl_category($cat);
87 $this->netConfigDNS->set_acl_category($cat);
88 }
91 function execute()
92 {
93 /* Call parent execute */
94 plugin::execute();
96 /* Do we represent a valid server? */
97 if (!$this->is_account && $this->parent == NULL){
98 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
99 _("This 'dn' has no server features.")."</b>";
100 return($display);
101 }
103 /* Check for action */
104 if (isset($_POST['action']) && $this->acl_is_writeable("FAIstate")){
105 $cmd= search_config($this->config->data['TABS'], "workgeneric", "ACTIONCMD");
106 if ($cmd == ""){
107 print_red(_("No ACTIONCMD definition found in your gosa.conf"));
108 } else {
110 exec ($cmd." ".$this->netConfigDNS->macAddress." ".escapeshellarg($_POST['saction']), $dummy, $retval);
111 if ($retval != 0){
112 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
113 } else {
114 $this->didAction= TRUE;
116 /* Set FAIstate */
117 if($this->fai_activated && $this->dn != "new"){
118 $ldap = $this->config->get_ldap_link();
119 $ldap->cd($this->config->current['BASE']);
120 $ldap->cat($this->dn,array("objectClass"));
121 $res = $ldap->fetch();
123 $attrs = array();
124 $attrs['FAIstate'] = "";
125 if(isset($this->mapActions[$_POST['saction']])){
126 $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
127 }
129 for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
130 $attrs['objectClass'][] = $res['objectClass'][$i];
131 }
133 if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
134 $attrs['objectClass'][] = "FAIobject";
135 }
137 if($attrs['FAIstate'] == ""){
138 #FIXME we should check if FAIobject is used anymore
139 $attrs['FAIstate'] = array();
140 }
142 $ldap->cd($this->dn);
143 $ldap->modify($attrs);
144 }
145 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system server/generic (FAIstate) with dn '%s' failed."),$this->dn));
146 }
147 }
148 }
150 /* Base select dialog */
151 $once = true;
152 foreach($_POST as $name => $value){
153 if(preg_match("/^chooseBase/",$name) && $once ){
154 $once = false;
155 $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
156 $this->dialog->setCurrentBase($this->base);
157 }
158 }
160 /* Dialog handling */
161 if(is_object($this->dialog)){
162 /* Must be called before save_object */
163 $this->dialog->save_object();
165 if($this->dialog->isClosed()){
166 $this->dialog = false;
167 }elseif($this->dialog->isSelected()){
169 /* Only accept allowed bases */
170 $tmp = $this->get_allowed_bases();
171 if($tmp[$this->dialog->isSelected()]){
172 $this->base = $this->dialog->isSelected();
173 }
174 $this->dialog= false;
175 }else{
176 return($this->dialog->execute());
177 }
178 }
180 /* Fill templating stuff */
181 $smarty= get_smarty();
183 $tmp = $this->plInfo();
184 foreach($tmp['plProvidedAcls'] as $name => $translated){
185 $smarty->assign($name."ACL",$this->getacl($name));
186 }
188 /* Assign base ACL */
189 $smarty->assign("bases" , $this->get_allowed_bases());
191 /* Assign attributes */
192 foreach ($this->attributes as $attr){
193 $smarty->assign("$attr", $this->$attr);
194 }
196 $smarty->assign("staticAddress", "");
197 $smarty->assign("base_select", $this->base);
199 /* Assign status */
200 $query= "fping -q -r 1 -t 500 ".$this->cn;
201 exec ($query, $dummy, $retval);
203 /* Offline */
204 if ($retval == 0){
205 $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
206 "instant_update" => _("Instant update"),
207 "update" => _("Scheduled update"),
208 "reinstall" => _("Reinstall"),
209 "rescan" => _("Rescan hardware"),
210 "memcheck" => _("Memory test"),
211 "localboot" => _("Force localboot"),
212 "sysinfo" => _("System analysis")));
213 } else {
214 $smarty->assign("actions", array("wake" => _("Wake up"),
215 "reinstall" => _("Reinstall"),
216 "update" => _("Scheduled update"),
217 "memcheck" => _("Memory test"),
218 "localboot" => _("Force localboot"),
219 "sysinfo" => _("System analysis")));
220 }
222 /* Show main page */
223 $smarty->assign("fai_activated",$this->fai_activated);
224 $smarty->assign("netconfig", $this->netConfigDNS->execute());
225 $smarty->assign("modes", $this->modes);
227 return($smarty->fetch (get_template_path('server.tpl', TRUE)));
228 }
230 function remove_from_parent()
231 {
232 /* Cancel if there's nothing to do here */
233 if ((!$this->initially_was_account) || (!$this->acl_is_removeable())){
234 return;
235 }
237 $this->netConfigDNS->remove_from_parent();
238 $ldap= $this->config->get_ldap_link();
239 $ldap->rmdir($this->dn);
241 @log::log("remove","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
243 show_ldap_error($ldap->get_error(), sprintf(_("Removing of system server/generic with dn '%s' failed."),$this->dn));
245 /* Delete references to object groups */
246 $ldap->cd ($this->config->current['BASE']);
247 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
248 while ($ldap->fetch()){
249 $og= new ogroup($this->config, $ldap->getDN());
250 unset($og->member[$this->dn]);
251 $og->save ();
252 }
253 $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
254 }
257 /* Save data to object */
258 function save_object()
259 {
260 /* Save current base, to be able to revert to last base,
261 if new base is invalid or not allowed to be selected */
262 $base_tmp = $this->base;
263 plugin::save_object();
264 $this->netConfigDNS->save_object();
266 /* Get base selection */
267 $tmp = $this->get_allowed_bases();
268 if(isset($_POST['base'])){
269 if($tmp[$_POST['base']]){
270 $this->base = $_POST['base'];
271 }else{
272 $this->base = $base_tmp;
273 }
274 }
275 }
278 /* Check supplied data */
279 function check()
280 {
281 /* Call common method to give check the hook */
282 $message= plugin::check();
283 $message= array_merge($message, $this->netConfigDNS->check());
284 $this->dn= "cn=".$this->cn.",ou=servers,ou=systems,".$this->base;
286 /* must: cn */
287 if ($this->cn == ""){
288 $message[]= _("The required field 'Server name' is not set.");
289 }
291 if ($this->orig_dn != $this->dn){
292 $ldap= $this->config->get_ldap_link();
293 $ldap->cd ($this->base);
294 $ldap->search ("(cn=".$this->cn.")", array("cn"));
295 if ($ldap->count() != 0){
296 while ($attrs= $ldap->fetch()){
297 if ($attrs['dn'] != $this->orig_dn){
298 if(!preg_match("/,ou=incoming,/",$attrs['dn'])){
299 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
300 break;
301 }
302 }
303 }
304 }
305 }
307 return ($message);
308 }
311 /* Save to LDAP */
312 function save()
313 {
314 plugin::save();
316 /* Remove all empty values */
317 if ($this->orig_dn == 'new'){
318 $attrs= array();
319 foreach ($this->attrs as $key => $val){
320 if (is_array($val) && count($val) == 0){
321 continue;
322 }
323 $attrs[$key]= $val;
324 }
325 $this->attrs= $attrs;
326 }
328 /* Write back to ldap */
329 $ldap= $this->config->get_ldap_link();
330 if ($this->orig_dn == 'new'){
331 $ldap->cd($this->config->current['BASE']);
332 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
333 $ldap->cd($this->dn);
334 $ldap->add($this->attrs);
335 @log::log("create","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
336 $mode= "add";
337 } else {
339 /* cn is not case sensitive for ldap, but for php it is!! */
340 if($this->config->current['DNMODE'] == "cn"){
341 if (strtolower($this->orig_dn) != (strtolower($this->dn))){
342 $this->move($this->orig_dn, $this->dn);
343 plugin::save();
344 }
345 }else{
346 if ($this->orig_dn != $this->dn){
347 $this->move($this->orig_dn, $this->dn);
348 plugin::save();
349 }
350 }
352 $ldap->cd($this->dn);
353 $this->cleanup();
354 $ldap->modify ($this->attrs);
355 @log::log("modify","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
357 $mode= "modify";
358 }
359 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system server/generic with dn '%s' failed."),$this->dn));
360 $this->netConfigDNS->cn = $this->cn;
361 $this->netConfigDNS->save($this->dn);
363 /* Optionally execute a command after we're done */
364 if(!$this->didAction){
365 $this->handle_post_events($mode,array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
366 }
367 }
370 /* Return plugin informations for acl handling */
371 function plInfo()
372 {
373 return (array(
374 "plShortName" => _("Generic"),
375 "plDescription" => _("Server generic"),
376 "plSelfModify" => FALSE,
377 "plDepends" => array(),
378 "plPriority" => 1,
379 "plSection" => array("administration"),
380 "plCategory" => array("server" => array("description" => _("Server"),
381 "objectClass" => "gotoWorkstation")),
382 "plProvidedAcls"=> array(
383 "cn" => _("Name"),
384 "description" => _("Description"),
385 "gotoMode" => _("Goto mode"),
386 "base" => _("Base"),
387 "FAIstate" => _("Action flag"))
388 ));
389 }
390 }
392 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
393 ?>