PageRenderTime 189ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/izpack-src/tags/milestone-3-7-0-M2/src/lib/com/izforge/izpack/panels/SudoPanel.java

https://github.com/jponge/izpack-full-svn-history-copy
Java | 188 lines | 110 code | 22 blank | 56 comment | 7 complexity | 004d272c166ab40c4b098a1c945c13a8 MD5 | raw file
  1. /*
  2. * $Id$
  3. * IzPack
  4. * Copyright (C) 2003 Jan Blok (jblok@profdata.nl - PDM - www.profdata.nl)
  5. *
  6. * File : SudoPanel.java
  7. * Description : A panel doing a linux/unix/macosx 'sudo' for administrator (native (sub)) installs.
  8. * Author's email : jblok@profdata.nl
  9. * Author's Website : http://www.profdata.nl
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version 2
  14. * of the License, or any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  24. */
  25. package com.izforge.izpack.panels;
  26. import com.izforge.izpack.*;
  27. import com.izforge.izpack.gui.LabelFactory;
  28. import com.izforge.izpack.installer.*;
  29. import com.izforge.izpack.util.FileExecutor;
  30. import com.izforge.izpack.util.OsConstraint;
  31. import java.awt.BorderLayout;
  32. import java.awt.Dimension;
  33. import java.awt.event.*;
  34. import java.io.File;
  35. import java.io.FileOutputStream;
  36. import java.util.*;
  37. import javax.swing.*;
  38. /**
  39. * The packs selection panel class.
  40. *
  41. * @author Jan Blok
  42. * @since November 27, 2003
  43. */
  44. public class SudoPanel extends IzPanel implements ActionListener
  45. {
  46. private JTextField passwordField;
  47. private boolean isValid = false;
  48. /**
  49. * The constructor.
  50. *
  51. * @param parent The parent window.
  52. * @param idata The installation data.
  53. */
  54. public SudoPanel(InstallerFrame parent, InstallData idata)
  55. {
  56. super(parent, idata);
  57. setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
  58. add(LabelFactory.create(
  59. /*parent.langpack.getString("SudoPanel.info")*/"For installing administrator privileges are necessary",
  60. JLabel.TRAILING));
  61. add(Box.createRigidArea(new Dimension(0, 5)));
  62. add(LabelFactory.create(
  63. /*parent.langpack.getString("SudoPanel.tip")*/"Please note that passwords are case-sensitive", parent.icons.getImageIcon("tip"),
  64. JLabel.TRAILING));
  65. add(Box.createRigidArea(new Dimension(0, 5)));
  66. JPanel spacePanel = new JPanel();
  67. spacePanel.setAlignmentX(LEFT_ALIGNMENT);
  68. spacePanel.setAlignmentY(CENTER_ALIGNMENT);
  69. spacePanel.setBorder(BorderFactory.createEmptyBorder(80, 30, 0, 50));
  70. spacePanel.setLayout(new BorderLayout(5,5));
  71. spacePanel.add(
  72. LabelFactory.create(
  73. /*parent.langpack.getString("SudoPanel.specifyAdminPassword")*/"Please specify your password:"),BorderLayout.NORTH);
  74. passwordField = new JPasswordField();
  75. passwordField.addActionListener(this);
  76. JPanel space2Panel = new JPanel();
  77. space2Panel.setLayout(new BorderLayout());
  78. space2Panel.add(passwordField,BorderLayout.NORTH);
  79. space2Panel.add(Box.createRigidArea(new Dimension(0, 5)),BorderLayout.CENTER);
  80. spacePanel.add(space2Panel,BorderLayout.CENTER);
  81. add(spacePanel);
  82. }
  83. /** Called when the panel becomes active. */
  84. public void panelActivate()
  85. {
  86. passwordField.requestFocus();
  87. }
  88. /**
  89. * Actions-handling method.
  90. *
  91. * @param e The event.
  92. */
  93. public void actionPerformed(ActionEvent e)
  94. {
  95. doSudoCmd();
  96. }
  97. //check if sudo password is correct (so sudo can be used in all other scripts, even without password, lasts for 5 minutes)
  98. private void doSudoCmd()
  99. {
  100. String pass = passwordField.getText();
  101. File file = null;
  102. try
  103. {
  104. //write file in /tmp
  105. file = new File("/tmp/cmd_sudo.sh");//""c:/temp/run.bat""
  106. FileOutputStream fos = new FileOutputStream(file);
  107. fos.write("echo $password | sudo -S ls\nexit $?".getBytes()); //"echo $password > pipo.txt"
  108. fos.close();
  109. //execute
  110. HashMap vars = new HashMap();
  111. vars.put("password", pass);
  112. List oses = new ArrayList();
  113. oses.add(new OsConstraint("unix",null,null,null));//"windows",System.getProperty("os.name"),System.getProperty("os.version"),System.getProperty("os.arch")));
  114. ArrayList plist = new ArrayList();
  115. ParsableFile pf = new ParsableFile(file.getAbsolutePath(),null,null,oses);
  116. plist.add(pf);
  117. ScriptParser sp = new ScriptParser(plist,new VariableSubstitutor(vars));
  118. sp.parseFiles();
  119. ArrayList elist = new ArrayList();
  120. ExecutableFile ef = new ExecutableFile(file.getAbsolutePath(),ExecutableFile.POSTINSTALL,ExecutableFile.ABORT,oses,false);
  121. elist.add(ef);
  122. FileExecutor fe = new FileExecutor(elist);
  123. int retval = fe.executeFiles(ExecutableFile.POSTINSTALL,this);
  124. if (retval == 0)
  125. {
  126. idata.setVariable("password", pass);
  127. isValid = true;
  128. }
  129. // else is already showing dialog
  130. // {
  131. // JOptionPane.showMessageDialog(this, "Cannot execute 'sudo' cmd, check your password", "Error", JOptionPane.ERROR_MESSAGE);
  132. // }
  133. }
  134. catch (Exception e)
  135. {
  136. // JOptionPane.showMessageDialog(this, "Cannot execute 'sudo' cmd, check your password", "Error", JOptionPane.ERROR_MESSAGE);
  137. e.printStackTrace();
  138. isValid = false;
  139. }
  140. try
  141. {
  142. if (file != null && file.exists()) file.delete();//you don't want the file with password tobe arround, in case of error
  143. }
  144. catch (Exception e)
  145. {
  146. //ignore
  147. }
  148. }
  149. /**
  150. * Indicates wether the panel has been validated or not.
  151. *
  152. * @return Always true.
  153. */
  154. public boolean isValidated()
  155. {
  156. if (!isValid)
  157. {
  158. doSudoCmd();
  159. }
  160. if (!isValid)
  161. {
  162. JOptionPane.showInternalMessageDialog(this, "Password", "Password is not valid", JOptionPane.ERROR_MESSAGE);
  163. }
  164. return isValid;
  165. }
  166. }