Code

Added bootstrap method detection
[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('kernel1','kk1');
26     public $installKeyboardlayout = "";
27     public $installKeyboardlayoutList = array("104","105");
29     public $installSystemLocale = "";
30     public $installSystemLocaleList = array('de','en_EN.UTF-8');
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 NTP servers list 
69         $this->installNTPServerList= new sortableListing($this->installNTPServer);
70         $this->installNTPServerList->setDeleteable(true);
71         $this->installNTPServerList->setEditable(false);
72         $this->installNTPServerList->setColspecs(array('*'));
73         $this->installNTPServerList->setWidth("100%");
74         $this->installNTPServerList->setHeight("70px");
76         // Init the object values.
77         $this->init();
78     }
80    
81     /*! \brief   Load attribute values from the GOsa-backend using jsonRPC 
82      */ 
83     function init()
84     {
85         // Detect account status by checking the used objectClasses
86         $this->is_account = FALSE;
87         foreach($this->map as $type){
88             if(in_array($type['objectClass'], $this->attrs['objectClass'])){
89                 $this->installBootstrapMethod = $type['bootstrap'];
90                 $this->installConfigManagement = $type['type'];
91                 $this->is_account = TRUE;
92             }
93         }
95         // Start without error.
96         $this->initFailed = FALSE;
99         // Prepare list of timezones
100         $this->timezones = $this->getTimezones();
102         // Get list of NTP servers
103         $this->installNTPServer = array();
104         if(isset($this->attrs['installNTPServer']['count'])){
105             for($i = 0; $i < $this->attrs['installNTPServer']['count']; $i++){
106                 $this->installNTPServer[] = $this->attrs['installNTPServer'][$i];
107             }
108         }
110         // Get list of password hashes
111         $tmp = passwordMethod::get_available_methods();
112         $this->hashes = array();
113         foreach($tmp['name'] as $name){
114             $this->hashes[$name] = $name;
115         }
117         // Load list of bootstrap methods.
118         if(!$this->loadInstallationMethods()){
119             $this->initFailed = TRUE;
120             msg_dialog(_("Error"), msgPool::rpcError($this->rpcErrorMessage), ERROR_DIALOG);
121             return;
122         }
123     
125         $this->installTemplateDNList = array(
126         'dc=intranet,dc=gonicus,dc=de' => 'Intranet-Tpl',
127         'ou=systems,dc=intranet,dc=gonicus,dc=de' => 'System-Tpl');
129         $this->installMirrorDNList = array(
130         'dc=intranet,dc=gonicus,dc=de' => 'Intranet',
131         'ou=systems,dc=intranet,dc=gonicus,dc=de' => 'System');
133         // Preset some values for new accounts
134         if(!$this->is_account){
136             // Preset the device timezone
137             $tz = timezone::get_default_timezone();
138             $this->installTimezone = $tz['name'];
140             // Set initial bootstrap and install method.
141             $this->installBootstrapMethod = key($this->installBootstrapMethodList);
142             $this->installConfigManagement = key($this->installConfigManagementList[$this->installBootstrapMethod]);
143         }
145         // Check if we've a valid bootstrap and configMangement method selected
146         if(!isset($this->installConfigManagementList[$this->installBootstrapMethod])){
147             $newMethod = key($this->installBootstrapMethodList);
148             msg_dialog::display(_("Error"), 
149                 sprintf(_("The selected bootstrap method '%s' is invalid! Using '%s' instead!"),
150                 $this->installBootstrapMethod,$newMethod), ERROR_DIALOG);
151             $this->installBootstrapMethod = $newMethod;
152         }
153         if(!isset($this->installConfigManagementList[$this->installBootstrapMethod][$this->installConfigManagement])){
154             $newMethod = key($this->installConfigManagementList[$this->installBootstrapMethod]);
155             msg_dialog::display(_("Error"), 
156                 sprintf(_("The selected config management method '%s' is invalid! Using '%s' instead!"),
157                 $this->installConfigManagement,$newMethod), ERROR_DIALOG);
158             $this->installConfigManagement = $newMethod;
159         }
160     }
163     function getTimezones()
164     {
165         $tmp = timezone::_get_tz_zones();
166         $list = array();
167         foreach($tmp['TIMEZONES'] as $name => $offset){
168             if($offset >= 0){
169                 $list[$name] = $name." ( + ".sprintf("%0.2f",$offset/(60*60))." "._("hours").")";
170             }else{
171                 $offset = $offset * -1;
172                 $list[$name] = $name." ( - ".sprintf("%0.2f",($offset/(60*60)))." "._("hours").")";
173             }
174         }  
175         uksort($list, 'strnatcasecmp'); 
176         return($list);
177     }
178    
180     /*! \brief  Try to load install and bootstrap methods from the GOsa-backend
181      *          This is done via jsonRPC, if calling the backend method fails,
182      *           this method returns FALSE, in case of success it return TRUE; 
183      */ 
184     function loadInstallationMethods()
185     { 
186         $this->installBootstrapMethodList = array();
187         $this->installConfigManagementList = array();
188         $rpc = $this->config->getRpcHandle();
189         $res = $rpc->getSupportedBaseInstallMethods();
190        
191         // Failed to load backend values, keep reported error and return with FALSE; 
192         $this->rpcError = !$rpc->success();
193         if(!$rpc->success()){
194             $this->rpcErrorMessage = $rpc->get_error();
195             return(FALSE);
196         }
198         // Walk through result and set bootstrap methods.
199         foreach($res as $name => $method){
200             $this->installBootstrapMethodList[$name] = $method['name'];
201             foreach($method['methods'] as $m){
202                 $this->installConfigManagementList[$name][$m] = $m; 
203             }
204         }
205         return(TRUE);
206     }
207     
209     function execute()
210     {
211         // Log account access
212         if($this->is_account && !$this->view_logged){
213             $this->view_logged = TRUE;
214             new log("view","Device/".get_class($this),$this->dn);
215         }
217         /***************
218           Handle account state
219          ***************/
221         // Allow to add or remove the distribution list extension
222         if(isset($_POST['modify_state'])){
223             if($this->is_account && $this->acl_is_removeable()){
224                 $this->is_account= FALSE;
225             }elseif(!$this->is_account && $this->acl_is_createable()){
226                 $this->is_account= TRUE;
227             }
228         }
230         // Show account status-changer
231         $display = "";
232         if ($this->parent !== NULL){
233             if ($this->is_account){
234                 $display= $this->show_disable_header(_("Remove instal profile"),
235                         msgPool::featuresEnabled(_("Install profile")));
236             } else {
237                 $display= $this->show_enable_header(_("Add install profile"),
238                         msgPool::featuresDisabled(_("Install profile")));
239                 return ($display);
240             }
241         }
243         /***************
244           Root password hash dialog
245          ***************/
247         if($this->setKickstartRootPasswordHash){
248             $this->dialog = TRUE;
249             $smarty = get_smarty();
250             $smarty->assign('hashes', set_post($this->hashes));
251             $smarty->assign('hash', set_post($this->hash));
252             return($smarty->fetch(get_template_path('goto/Device/SetPassword.tpl', TRUE)));
253         }
255         /***************
256           Generate HTML content
257          ***************/
259         $this->installNTPServerList->setAcl($this->getacl('installNTPServer'));
260         $this->installNTPServerList->update();
262         plugin::execute();    
263         $smarty = get_smarty();
264         $smarty->assign('timezones', $this->timezones);
265         $smarty->assign('installKeyboardlayoutList', $this->installKeyboardlayoutList);
266         $smarty->assign('installKernelPackageList', $this->installKernelPackageList);
267         $smarty->assign('installTemplateDNList', $this->installTemplateDNList);
268         $smarty->assign('installMirrorDNList', $this->installMirrorDNList);
269         $smarty->assign('installSystemLocaleList', $this->installSystemLocaleList);
270         $smarty->assign('installNTPServerList', $this->installNTPServerList->render());
272         $smarty->assign('installBootstrapMethod', $this->installBootstrapMethod);
273         $smarty->assign('installBootstrapMethodList', $this->installBootstrapMethodList);
274         $smarty->assign('installConfigManagement', $this->installConfigManagement);
276         $cfgList = $this->installConfigManagementList[$this->installBootstrapMethod];
277         $smarty->assign('installConfigManagementList', $cfgList);
278         foreach($this->attributes as $attr){
279             $smarty->assign($attr, $this->$attr);
280         }
281         $this->dialog = false;
282         return($display.$smarty->fetch(get_template_path('goto/Device/InstallRecipe.tpl', TRUE)));
283     }
286     function save_object()
287     {
288         if(isset($_POST['InstallRecipePosted'])){
290             $currentInstallMethod = $this->installConfigManagement;
291             plugin::save_object();
292             $this->installRootEnabled = isset($_POST['installRootEnabled']);
293             $this->installTimeUTC = isset($_POST['installTimeUTC']);
296             if(isset($_POST['setKickstartRootPasswordHash'])){
297                 $this->setKickstartRootPasswordHash = true;
298             }
300             $this->installNTPServerList->save_object();
301             $this->installNTPServer = $this->installNTPServerList->getMaintainedData();
302             if(isset($_POST['installNTPServer_Input']) && isset($_POST['installNTPServer_Add'])){
303                 $add = get_post('installNTPServer_Input');
304                 if(!in_array($add, $this->installNTPServer) && !empty($add)){
305                     $this->installNTPServer[] = $add;
306                 }
307             }
308             $this->installNTPServerList->setListData($this->installNTPServer);
309         }
310         if(isset($_POST['cancelPassword'])) $this->setKickstartRootPasswordHash =false;
311         if(isset($_POST['setPassword'])) {
312             $this->setKickstartRootPasswordHash =false;
313             $hash = get_post('passwordHash');
315             // Not sure, why this is here, but maybe some encryption methods require it.
316             mt_srand((double) microtime()*1000000);
318             // Generate the requested hash
319             $methods = new passwordMethod($this->config, $this->dn);
320             $available = $methods->get_available_methods();
321             $test = new $available[$hash]($this->config,$this->dn);
322             $this->installRootPasswordHash = @$test->generate_hash(get_post('rootPassword'));
323         }
325     }
328     function save()
329     {
330         // 
331         if(!$this->installRootEnabled) $this->installRootPasswordHash = "";
332         $this->installRootEnabled = ($this->installRootEnabled)?'TRUE':'FALSE';
333         $this->installTimeUTC = ($this->installTimeUTC)?'TRUE':'FALSE';
334         
335         plugin::save();
337         unset($this->attrs['installConfigManagement']);
338         unset($this->attrs['installBootstrapMethod']);
339         echo "Skipp saving of 'installConfigManagement' and 'installBootstrapMethod', i've no idea where to store them.";
341         // Remove all objectClasses which belongs to a bootstrap method, 
342         //  and then add the selected one.
343         foreach($this->map as $type){
344             $this->attrs['objectClass'] = array_remove_entries_ics(array($type['objectClass']), $this->attrs['objectClass']);
345         }
346         $this->attrs['objectClass'][] = $this->map[$this->installConfigManagement]['objectClass'];
347         
348         print_a($this->attrs);
350         $this->cleanup();
351         $ldap=$this->config->get_ldap_link();
352         $ldap->cd($this->config->current['BASE']);
353         $ldap->cd($this->dn);
354         $ldap->modify($this->attrs);
355         new log("modify","Device/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
356         if (!$ldap->success()){
357             msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
358         }else{
359             $this->handle_post_events("modify");
360         }
361     }
363     
364     function remove_from_parent()
365     {
366         plugin::remove_from_parent();
367         unset($this->attrs['installConfigManagement']);
368         unset($this->attrs['installBootstrapMethod']);
369         echo "Skipp saving of 'installConfigManagement' and 'installBootstrapMethod', i've no idea where to store them.";
371         $ldap=$this->config->get_ldap_link();
372         $ldap->cd($this->config->current['BASE']);
373         $ldap->cd($this->dn);
374         $ldap->modify($this->attrs);
375         new log("remove","Device/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
376         if (!$ldap->success()){
377             msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
378         }else{
379             $this->handle_post_events("remove");
380         }
381         
382     }
383     
385     static function plInfo()
386     {
387         return (array(
388                     "plShortName"   => _("Device"),
389                     "plDescription" => _("Registered device"),
390                     "plSelfModify"  => FALSE,
391                     "plDepends"     => array(),
392                     "plPriority"    => 1,
393                     "plSection"     => array("administration"),
394                     "plCategory"    => array("Device"),
395                     "plProvidedAcls" => array(
396                         "member" => _("Member"),
397                         "installTemplateDN" => _("Template"),
398                         "installBootstrapMethod" => _("Bootstrap method"),
399                         "installConfigManagement" => _("Config management"),
400                         "installKeyboardlayout" => _("Keyboard layout"),
401                         "installSystemLocale" => _("System locale"),
402                         "installTimezone" => _("Timezone"),
403                         "installTimeUTC" => _("Time"),
404                         "installNTPServer" => _("NTP-Server"),
405                         "installMirrorDN" => _("Kickstart mirror"),
406                         "installRootEnabled" => _("Root login enabled"),
407                         "installRootPasswordHash" => _("Root password hash"),
408                         "installKernelPackage" => _("Kernal package"),
409                         "installPartitionTable" => _("Partition table")
410                         )
411                         )
412                         );
413     }
416 ?>