1 21 22 package com.izforge.izpack.panels; 23 24 import java.awt.BorderLayout ; 25 import java.awt.Dimension ; 26 import java.awt.event.ActionEvent ; 27 import java.awt.event.ActionListener ; 28 import java.io.File ; 29 import java.io.FileOutputStream ; 30 import java.util.ArrayList ; 31 import java.util.List ; 32 import java.util.Properties ; 33 34 import javax.swing.BorderFactory ; 35 import javax.swing.Box ; 36 import javax.swing.BoxLayout ; 37 import javax.swing.JLabel ; 38 import javax.swing.JOptionPane ; 39 import javax.swing.JPanel ; 40 import javax.swing.JPasswordField ; 41 import javax.swing.JTextField ; 42 43 import com.izforge.izpack.ExecutableFile; 44 import com.izforge.izpack.ParsableFile; 45 import com.izforge.izpack.gui.LabelFactory; 46 import com.izforge.izpack.installer.InstallData; 47 import com.izforge.izpack.installer.InstallerFrame; 48 import com.izforge.izpack.installer.IzPanel; 49 import com.izforge.izpack.installer.ScriptParser; 50 import com.izforge.izpack.util.FileExecutor; 51 import com.izforge.izpack.util.OsConstraint; 52 import com.izforge.izpack.util.VariableSubstitutor; 53 54 60 public class SudoPanel extends IzPanel implements ActionListener 61 { 62 63 66 private static final long serialVersionUID = 3689628116465561651L; 67 68 private JTextField passwordField; 69 70 private boolean isValid = false; 71 72 78 public SudoPanel(InstallerFrame parent, InstallData idata) 79 { 80 super(parent, idata); 81 82 setLayout(new BoxLayout (this, BoxLayout.Y_AXIS)); 83 84 add(LabelFactory 85 .create( 86 "For installing administrator privileges are necessary", 87 JLabel.TRAILING)); 88 89 add(Box.createRigidArea(new Dimension (0, 5))); 90 91 add(LabelFactory 92 .create( 93 "Please note that passwords are case-sensitive", 94 parent.icons.getImageIcon("tip"), JLabel.TRAILING)); 95 96 add(Box.createRigidArea(new Dimension (0, 5))); 97 98 JPanel spacePanel = new JPanel (); 99 spacePanel.setAlignmentX(LEFT_ALIGNMENT); 100 spacePanel.setAlignmentY(CENTER_ALIGNMENT); 101 spacePanel.setBorder(BorderFactory.createEmptyBorder(80, 30, 0, 50)); 102 spacePanel.setLayout(new BorderLayout (5, 5)); 103 spacePanel 104 .add( 105 LabelFactory 106 .create( 107 "Please specify your password:"), 108 BorderLayout.NORTH); 109 passwordField = new JPasswordField (); 110 passwordField.addActionListener(this); 111 JPanel space2Panel = new JPanel (); 112 space2Panel.setLayout(new BorderLayout ()); 113 space2Panel.add(passwordField, BorderLayout.NORTH); 114 space2Panel.add(Box.createRigidArea(new Dimension (0, 5)), BorderLayout.CENTER); 115 spacePanel.add(space2Panel, BorderLayout.CENTER); 116 add(spacePanel); 117 } 118 119 120 public void panelActivate() 121 { 122 passwordField.requestFocus(); 123 } 124 125 130 public void actionPerformed(ActionEvent e) 131 { 132 doSudoCmd(); 133 } 134 135 private void doSudoCmd() 138 { 139 String pass = passwordField.getText(); 140 141 File file = null; 142 try 143 { 144 file = new File ("/tmp/cmd_sudo.sh"); FileOutputStream fos = new FileOutputStream (file); 147 fos.write("echo $password | sudo -S ls\nexit $?".getBytes()); fos.close(); 152 153 Properties vars = new Properties (); 155 vars.put("password", pass); 156 157 List oses = new ArrayList (); 158 oses.add(new OsConstraint("unix", null, null, null)); 159 160 ArrayList plist = new ArrayList (); 161 ParsableFile pf = new ParsableFile(file.getAbsolutePath(), null, null, oses); 162 plist.add(pf); 163 ScriptParser sp = new ScriptParser(plist, new VariableSubstitutor(vars)); 164 sp.parseFiles(); 165 166 ArrayList elist = new ArrayList (); 167 ExecutableFile ef = new ExecutableFile(file.getAbsolutePath(), 168 ExecutableFile.POSTINSTALL, ExecutableFile.ABORT, oses, false); 169 elist.add(ef); 170 FileExecutor fe = new FileExecutor(elist); 171 int retval = fe.executeFiles(ExecutableFile.POSTINSTALL, this); 172 if (retval == 0) 173 { 174 idata.setVariable("password", pass); 175 isValid = true; 176 } 177 } 183 catch (Exception e) 184 { 185 e.printStackTrace(); 188 isValid = false; 189 } 190 try 191 { 192 if (file != null && file.exists()) file.delete(); } 199 catch (Exception e) 200 { 201 } 203 } 204 205 210 public boolean isValidated() 211 { 212 if (!isValid) 213 { 214 doSudoCmd(); 215 } 216 if (!isValid) 217 { 218 JOptionPane.showInternalMessageDialog(this, "Password", "Password is not valid", 219 JOptionPane.ERROR_MESSAGE); 220 } 221 return isValid; 222 } 223 } 224 | Popular Tags |