Code

7e73df9470a1fd29226061708e5d4665798510e4
[gosa.git] / gosa-plugins / goto / admin / systems / goto / Device / class_InstallRecipe.inc
1 <?php
3 class InstallRecipe extends plugin
4 {
6     /* This has to be replaced by valid attributes, currently they are just 
7      *  fake variables. #FIXME: I do not know the real attribute names yet.
8      */
9     public $installBootstrapMethod = '';
10     public $installBootstrapMethodList = NULL;
12     public $installConfigManagement = '';
13     public $installConfigManagementList = NULL;
15     /* Plugin attributes
16      */
17     public $installTemplateDN = "";
18     public $installTemplateDNList = array();
20     public $installMirrorDN = "";
21     public $installMirrorDNList = array();
23     public $installKernelPackage = "";
24     public $installKernelPackageList = array();
26     public $installKeyboardlayout = "";
27     public $installKeyboardlayoutList = array();
29     public $installSystemLocale = "";
30     public $installSystemLocaleList = array();
32     public $installTimezone = "";
33     public $installTimeUTC = "";
35     public $installNTPServer = array();
36     public $installRootEnabled = "";
37     public $installRootPasswordHash = "";
39     public $setKickstartRootPasswordHash = FALSE;
40     public $installPartitionTable = "";
41     private $installNTPServerList = NULL;
43     public $objectclasses = array('installRecipe');
44     public $attributes = array("installTemplateDN","installKeyboardlayout","installSystemLocale",
45             "installTimezone","installTimeUTC","installNTPServer","installMirrorDN",
46             "installRootEnabled","installRootPasswordHash","installKernelPackage",
47             "installPartitionTable","installConfigManagement","installBootstrapMethod");
49     private $map = array('puppet' => array('objectClass' => 'puppetClient', 'type' => 'puppet', 'bootstrap' => 'preseed'));
51     /* Some plugin management related attributes 
52      * See class plugin for details.
53      */
54     public $view_logged = FALSE;
55     public $ignore_account = FALSE;
58     /*! \brief  Initialize the plugin and its widgets. 
59      *          Finally call init() to load values from the backend.
60      */ 
61     function __construct(&$config, $dn)
62     {
63         plugin::plugin($config, $dn);
65         // Set default password hash
66         $this->hash = $this->config->get_cfg_value("core","passwordDefaultHash"); 
68         // Prepare list of timezones
69         $this->timezones = $this->getTimezones();
71         // Get list of password hashes
72         $tmp = passwordMethod::get_available_methods();
73         $this->hashes = array();
74         foreach($tmp['name'] as $name){
75             $this->hashes[$name] = $name;
76         }
78         // Prepare NTP servers list 
79         $this->installNTPServerList= new sortableListing($this->installNTPServer);
80         $this->installNTPServerList->setDeleteable(true);
81         $this->installNTPServerList->setEditable(false);
82         $this->installNTPServerList->setColspecs(array('*'));
83         $this->installNTPServerList->setWidth("100%");
84         $this->installNTPServerList->setHeight("70px");
86         // Init the object values.
87         $this->init();
88     }
90    
91     /*! \brief   Load attribute values from the GOsa-backend using jsonRPC 
92      */ 
93     function init()
94     {
95         // Detect account status by checking the used objectClasses
96         $this->is_account = FALSE;
97         foreach($this->map as $type){
98             if(in_array($type['objectClass'], $this->attrs['objectClass'])){
99                 $this->installBootstrapMethod = $type['bootstrap'];
100                 $this->installConfigManagement = $type['type'];
101                 $this->is_account = TRUE;
102             }
103         }
105         // Start without error.
106         $this->initFailed = FALSE;
108         // Get list of NTP servers
109         $this->installNTPServer = array();
110         if(isset($this->attrs['installNTPServer']['count'])){
111             for($i = 0; $i < $this->attrs['installNTPServer']['count']; $i++){
112                 $this->installNTPServer[] = $this->attrs['installNTPServer'][$i];
113             }
114         }
116         // Load list of bootstrap methods.
117         $res = $this->loadInstallationMethods();
118         if($res == NULL){
119             $this->initFailed = TRUE;
120             msg_dialog(_("Error"), msgPool::rpcError($this->rpcErrorMessage), ERROR_DIALOG);
121             return;
122         }
123         $this->installBootstrapMethodList = $res['installBootstrapMethodList'];
124         $this->installConfigManagementList = $res['installConfigManagementList'];
127         // Load system locales 
128         $locales = $this->getSystemLocales();
129         if($locales == NULL){
130             $this->initFailed = TRUE;
131             msg_dialog(_("Error"), msgPool::rpcError($this->rpcErrorMessage), ERROR_DIALOG);
132             return;
133         }
134         $this->installSystemLocaleList = $locales;
137         // Load installation templates
138         $res = $this->loadInstallationTemplates();
139         if($res == NULL){
140             $this->initFailed = TRUE;
141             msg_dialog(_("Error"), msgPool::rpcError($this->rpcErrorMessage), ERROR_DIALOG);
142             return;
143         }
144         $this->installTemplateDNList = $res;
147         // Load installation keyboard layouts
148         $res = $this->loadInstallationKeyboardLayouts();
149         if($res == NULL){
150             $this->initFailed = TRUE;
151             msg_dialog(_("Error"), msgPool::rpcError($this->rpcErrorMessage), ERROR_DIALOG);
152             return;
153         }
154         $this->installKeyboardlayoutList = $res;
157         // Load kernel package list
158         $res = $this->loadInstallationKernelPackages();
159         if($res == NULL){
160             $this->initFailed = TRUE;
161             msg_dialog(_("Error"), msgPool::rpcError($this->rpcErrorMessage), ERROR_DIALOG);
162             return;
163         }
164         $this->installKernelPackageList = $res;
167         // Load installation mirrors
168         $res = $this->loadInstallationMirrors();
169         if($res == NULL){
170             $this->initFailed = TRUE;
171             msg_dialog(_("Error"), msgPool::rpcError($this->rpcErrorMessage), ERROR_DIALOG);
172             return;
173         }
174         $this->installMirrorDNList = $res;
177         // Preset some values for new accounts
178         if(!$this->is_account){
180             // Preset the device timezone
181             $tz = timezone::get_default_timezone();
182             $this->installTimezone = $tz['name'];
184             // Set initial bootstrap and install method.
185             $this->installBootstrapMethod = key($this->installBootstrapMethodList);
186             $this->installConfigManagement = key($this->installConfigManagementList[$this->installBootstrapMethod]);
187         }
190         // Check if we've a valid bootstrap and configMangement method selected
191         if(!isset($this->installConfigManagementList[$this->installBootstrapMethod])){
192             $newMethod = key($this->installBootstrapMethodList);
193             msg_dialog::display(_("Error"), 
194                 sprintf(_("The selected bootstrap method '%s' is invalid! Using '%s' instead!"),
195                 $this->installBootstrapMethod,$newMethod), ERROR_DIALOG);
196             $this->installBootstrapMethod = $newMethod;
197         }
198         if(!isset($this->installConfigManagementList[$this->installBootstrapMethod][$this->installConfigManagement])){
199             $newMethod = key($this->installConfigManagementList[$this->installBootstrapMethod]);
200             msg_dialog::display(_("Error"), 
201                 sprintf(_("The selected config management method '%s' is invalid! Using '%s' instead!"),
202                 $this->installConfigManagement,$newMethod), ERROR_DIALOG);
203             $this->installConfigManagement = $newMethod;
204         }
205     }
208     function getTimezones()
209     {
210         $tmp = timezone::_get_tz_zones();
211         $list = array();
212         foreach($tmp['TIMEZONES'] as $name => $offset){
213             if($offset >= 0){
214                 $list[$name] = $name." ( + ".sprintf("%0.2f",$offset/(60*60))." "._("hours").")";
215             }else{
216                 $offset = $offset * -1;
217                 $list[$name] = $name." ( - ".sprintf("%0.2f",($offset/(60*60)))." "._("hours").")";
218             }
219         }  
220         uksort($list, 'strnatcasecmp'); 
221         return($list);
222     }
223  
224     
225     function loadInstallationKernelPackages()
226     {
227         return(array('kernel1','kk1'));
228     }
229     
231     function loadInstallationMirrors()
232     {
233         $res = array(
234                 'dc=intranet,dc=gonicus,dc=de' => 'Intranet',
235                 'ou=systems,dc=intranet,dc=gonicus,dc=de' => 'System');
236         return($res);
237     }
239     function loadInstallationTemplates()
240     {
241         $res = array(
242                 'dc=intranet,dc=gonicus,dc=de' => 'Intranet-Tpl',
243                 'ou=systems,dc=intranet,dc=gonicus,dc=de' => 'System-Tpl');
244         return($res);
245     } 
247     function loadInstallationKeyboardLayouts()
248     {
249         return(array('104','105'));
250     } 
252     function getSystemLocales()
253     {
254         $locales = json_decode('{"hu_HU.UTF-8": "Hungarian", 
255                 "es_DO.UTF-8": "Spanish (Dominican Republic)", 
256                 "es_CL.UTF-8": "Spanish (Chile)"}');
258         return($locales);
259     }
260  
262     /*! \brief  Try to load install and bootstrap methods from the GOsa-backend
263      *          This is done via jsonRPC, if calling the backend method fails,
264      *           this method returns FALSE, in case of success it return TRUE; 
265      */ 
266     function loadInstallationMethods()
267     { 
268         $rpc = $this->config->getRpcHandle();
269         $res = $rpc->getSupportedBaseInstallMethods();
270        
271         // Failed to load backend values, keep reported error and return with FALSE; 
272         $this->rpcError = !$rpc->success();
273         if(!$rpc->success()){
274             $this->rpcErrorMessage = $rpc->get_error();
275             return(NULL);
276         }
278         // Walk through result and set bootstrap methods.
279         foreach($res as $name => $method){
280             $res['installBootstrapMethodList'][$name] = $method['name'];
281             foreach($method['methods'] as $m){
282                 $res['installConfigManagementList'][$name][$m] = $m; 
283             }
284         }
285         return($res);
286     }
287     
289     function execute()
290     {
291         // Log account access
292         if($this->is_account && !$this->view_logged){
293             $this->view_logged = TRUE;
294             new log("view","Device/".get_class($this),$this->dn);
295         }
297         /***************
298           Handle account state
299          ***************/
301         // Allow to add or remove the distribution list extension
302         if(isset($_POST['modify_state'])){
303             if($this->is_account && $this->acl_is_removeable()){
304                 $this->is_account= FALSE;
305             }elseif(!$this->is_account && $this->acl_is_createable()){
306                 $this->is_account= TRUE;
307             }
308         }
310         // Show account status-changer
311         $display = "";
312         if ($this->parent !== NULL){
313             if ($this->is_account){
314                 $display= $this->show_disable_header(_("Remove instal profile"),
315                         msgPool::featuresEnabled(_("Install profile")));
316             } else {
317                 $display= $this->show_enable_header(_("Add install profile"),
318                         msgPool::featuresDisabled(_("Install profile")));
319                 return ($display);
320             }
321         }
323         /***************
324           Root password hash dialog
325          ***************/
327         if($this->setKickstartRootPasswordHash){
328             $this->dialog = TRUE;
329             $smarty = get_smarty();
330             $smarty->assign('hashes', set_post($this->hashes));
331             $smarty->assign('hash', set_post($this->hash));
332             return($smarty->fetch(get_template_path('goto/Device/SetPassword.tpl', TRUE)));
333         }
335         /***************
336           Generate HTML content
337          ***************/
339         $this->installNTPServerList->setAcl($this->getacl('installNTPServer'));
340         $this->installNTPServerList->update();
342         plugin::execute();    
343         $smarty = get_smarty();
344         $smarty->assign('timezones', $this->timezones);
345         $smarty->assign('installKeyboardlayoutList', $this->installKeyboardlayoutList);
346         $smarty->assign('installKernelPackageList', $this->installKernelPackageList);
347         $smarty->assign('installTemplateDNList', $this->installTemplateDNList);
348         $smarty->assign('installMirrorDNList', $this->installMirrorDNList);
349         $smarty->assign('installSystemLocaleList', $this->installSystemLocaleList);
350         $smarty->assign('installNTPServerList', $this->installNTPServerList->render());
352         $smarty->assign('installBootstrapMethod', $this->installBootstrapMethod);
353         $smarty->assign('installBootstrapMethodList', $this->installBootstrapMethodList);
354         $smarty->assign('installConfigManagement', $this->installConfigManagement);
356         $cfgList = $this->installConfigManagementList[$this->installBootstrapMethod];
357         $smarty->assign('installConfigManagementList', $cfgList);
358         foreach($this->attributes as $attr){
359             $smarty->assign($attr, $this->$attr);
360         }
361         $this->dialog = false;
362         return($display.$smarty->fetch(get_template_path('goto/Device/InstallRecipe.tpl', TRUE)));
363     }
366     function save_object()
367     {
368         if(isset($_POST['InstallRecipePosted'])){
370             $currentInstallMethod = $this->installConfigManagement;
371             plugin::save_object();
372             $this->installRootEnabled = isset($_POST['installRootEnabled']);
373             $this->installTimeUTC = isset($_POST['installTimeUTC']);
376             if(isset($_POST['setKickstartRootPasswordHash'])){
377                 $this->setKickstartRootPasswordHash = true;
378             }
380             $this->installNTPServerList->save_object();
381             $this->installNTPServer = $this->installNTPServerList->getMaintainedData();
382             if(isset($_POST['installNTPServer_Input']) && isset($_POST['installNTPServer_Add'])){
383                 $add = get_post('installNTPServer_Input');
384                 if(!in_array($add, $this->installNTPServer) && !empty($add)){
385                     $this->installNTPServer[] = $add;
386                 }
387             }
388             $this->installNTPServerList->setListData($this->installNTPServer);
389         }
390         if(isset($_POST['cancelPassword'])) $this->setKickstartRootPasswordHash =false;
391         if(isset($_POST['setPassword'])) {
392             $this->setKickstartRootPasswordHash =false;
393             $hash = get_post('passwordHash');
395             // Not sure, why this is here, but maybe some encryption methods require it.
396             mt_srand((double) microtime()*1000000);
398             // Generate the requested hash
399             $methods = new passwordMethod($this->config, $this->dn);
400             $available = $methods->get_available_methods();
401             $test = new $available[$hash]($this->config,$this->dn);
402             $this->installRootPasswordHash = @$test->generate_hash(get_post('rootPassword'));
403         }
405     }
408     function save()
409     {
410         // 
411         if(!$this->installRootEnabled) $this->installRootPasswordHash = "";
412         $this->installRootEnabled = ($this->installRootEnabled)?'TRUE':'FALSE';
413         $this->installTimeUTC = ($this->installTimeUTC)?'TRUE':'FALSE';
414         
415         plugin::save();
417         unset($this->attrs['installConfigManagement']);
418         unset($this->attrs['installBootstrapMethod']);
419         echo "Skipp saving of 'installConfigManagement' and 'installBootstrapMethod', i've no idea where to store them.";
421         // Remove all objectClasses which belongs to a bootstrap method, 
422         //  and then add the selected one.
423         foreach($this->map as $type){
424             $this->attrs['objectClass'] = array_remove_entries_ics(array($type['objectClass']), $this->attrs['objectClass']);
425         }
426         $this->attrs['objectClass'][] = $this->map[$this->installConfigManagement]['objectClass'];
427         
428         print_a($this->attrs);
430         $this->cleanup();
431         $ldap=$this->config->get_ldap_link();
432         $ldap->cd($this->config->current['BASE']);
433         $ldap->cd($this->dn);
434         $ldap->modify($this->attrs);
435         new log("modify","Device/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
436         if (!$ldap->success()){
437             msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
438         }else{
439             $this->handle_post_events("modify");
440         }
441     }
443     
444     function remove_from_parent()
445     {
446         plugin::remove_from_parent();
447         unset($this->attrs['installConfigManagement']);
448         unset($this->attrs['installBootstrapMethod']);
449         echo "Skipp saving of 'installConfigManagement' and 'installBootstrapMethod', i've no idea where to store them.";
451         $ldap=$this->config->get_ldap_link();
452         $ldap->cd($this->config->current['BASE']);
453         $ldap->cd($this->dn);
454         $ldap->modify($this->attrs);
455         new log("remove","Device/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
456         if (!$ldap->success()){
457             msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
458         }else{
459             $this->handle_post_events("remove");
460         }
461         
462     }
463     
465     static function plInfo()
466     {
467         return (array(
468                     "plShortName"   => _("Device"),
469                     "plDescription" => _("Registered device"),
470                     "plSelfModify"  => FALSE,
471                     "plDepends"     => array(),
472                     "plPriority"    => 1,
473                     "plSection"     => array("administration"),
474                     "plCategory"    => array("Device"),
475                     "plProvidedAcls" => array(
476                         "member" => _("Member"),
477                         "installTemplateDN" => _("Template"),
478                         "installBootstrapMethod" => _("Bootstrap method"),
479                         "installConfigManagement" => _("Config management"),
480                         "installKeyboardlayout" => _("Keyboard layout"),
481                         "installSystemLocale" => _("System locale"),
482                         "installTimezone" => _("Timezone"),
483                         "installTimeUTC" => _("Time"),
484                         "installNTPServer" => _("NTP-Server"),
485                         "installMirrorDN" => _("Kickstart mirror"),
486                         "installRootEnabled" => _("Root login enabled"),
487                         "installRootPasswordHash" => _("Root password hash"),
488                         "installKernelPackage" => _("Kernal package"),
489                         "installPartitionTable" => _("Partition table")
490                         )
491                         )
492                         );
493     }
496 ?>