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