001: /*
002: * IzPack - Copyright 2001-2008 Julien Ponge, All Rights Reserved.
003: *
004: * http://izpack.org/
005: * http://izpack.codehaus.org/
006: *
007: * Copyright 2003 Jan Blok
008: *
009: * Licensed under the Apache License, Version 2.0 (the "License");
010: * you may not use this file except in compliance with the License.
011: * You may obtain a copy of the License at
012: *
013: * http://www.apache.org/licenses/LICENSE-2.0
014: *
015: * Unless required by applicable law or agreed to in writing, software
016: * distributed under the License is distributed on an "AS IS" BASIS,
017: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018: * See the License for the specific language governing permissions and
019: * limitations under the License.
020: */
021:
022: package com.izforge.izpack.panels;
023:
024: import java.awt.BorderLayout;
025: import java.awt.Dimension;
026: import java.awt.event.ActionEvent;
027: import java.awt.event.ActionListener;
028: import java.io.File;
029: import java.io.FileOutputStream;
030: import java.util.ArrayList;
031: import java.util.List;
032: import java.util.Properties;
033:
034: import javax.swing.BorderFactory;
035: import javax.swing.Box;
036: import javax.swing.BoxLayout;
037: import javax.swing.JLabel;
038: import javax.swing.JOptionPane;
039: import javax.swing.JPanel;
040: import javax.swing.JPasswordField;
041: import javax.swing.JTextField;
042:
043: import com.izforge.izpack.ExecutableFile;
044: import com.izforge.izpack.ParsableFile;
045: import com.izforge.izpack.gui.LabelFactory;
046: import com.izforge.izpack.installer.InstallData;
047: import com.izforge.izpack.installer.InstallerFrame;
048: import com.izforge.izpack.installer.IzPanel;
049: import com.izforge.izpack.installer.ScriptParser;
050: import com.izforge.izpack.util.FileExecutor;
051: import com.izforge.izpack.util.OsConstraint;
052: import com.izforge.izpack.util.VariableSubstitutor;
053:
054: /**
055: * The packs selection panel class.
056: *
057: * @author Jan Blok
058: * @since November 27, 2003
059: */
060: public class SudoPanel extends IzPanel implements ActionListener {
061:
062: /**
063: *
064: */
065: private static final long serialVersionUID = 3689628116465561651L;
066:
067: private JTextField passwordField;
068:
069: private boolean isValid = false;
070:
071: /**
072: * The constructor.
073: *
074: * @param parent The parent window.
075: * @param idata The installation data.
076: */
077: public SudoPanel(InstallerFrame parent, InstallData idata) {
078: super (parent, idata);
079:
080: setLayout(new BoxLayout(this , BoxLayout.Y_AXIS));
081:
082: add(LabelFactory
083: .create(
084: /* parent.langpack.getString("SudoPanel.info") */"For installing administrator privileges are necessary",
085: JLabel.TRAILING));
086:
087: add(Box.createRigidArea(new Dimension(0, 5)));
088:
089: add(LabelFactory
090: .create(
091: /* parent.langpack.getString("SudoPanel.tip") */"Please note that passwords are case-sensitive",
092: parent.icons.getImageIcon("tip"),
093: JLabel.TRAILING));
094:
095: add(Box.createRigidArea(new Dimension(0, 5)));
096:
097: JPanel spacePanel = new JPanel();
098: spacePanel.setAlignmentX(LEFT_ALIGNMENT);
099: spacePanel.setAlignmentY(CENTER_ALIGNMENT);
100: spacePanel.setBorder(BorderFactory.createEmptyBorder(80, 30, 0,
101: 50));
102: spacePanel.setLayout(new BorderLayout(5, 5));
103: spacePanel
104: .add(
105: LabelFactory
106: .create(
107: /* parent.langpack.getString("SudoPanel.specifyAdminPassword") */"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)),
115: BorderLayout.CENTER);
116: spacePanel.add(space2Panel, BorderLayout.CENTER);
117: add(spacePanel);
118: }
119:
120: /** Called when the panel becomes active. */
121: public void panelActivate() {
122: passwordField.requestFocus();
123: }
124:
125: /**
126: * Actions-handling method.
127: *
128: * @param e The event.
129: */
130: public void actionPerformed(ActionEvent e) {
131: doSudoCmd();
132: }
133:
134: // check if sudo password is correct (so sudo can be used in all other
135: // scripts, even without password, lasts for 5 minutes)
136: private void doSudoCmd() {
137: String pass = passwordField.getText();
138:
139: File file = null;
140: try {
141: // write file in /tmp
142: file = new File("/tmp/cmd_sudo.sh");// ""c:/temp/run.bat""
143: FileOutputStream fos = new FileOutputStream(file);
144: fos
145: .write("echo $password | sudo -S ls\nexit $?"
146: .getBytes()); // "echo
147: // $password
148: // >
149: // pipo.txt"
150: fos.close();
151:
152: // execute
153: Properties vars = new Properties();
154: vars.put("password", pass);
155:
156: List<OsConstraint> oses = new ArrayList<OsConstraint>();
157: oses.add(new OsConstraint("unix", null, null, null));
158:
159: ArrayList<ParsableFile> plist = new ArrayList<ParsableFile>();
160: ParsableFile pf = new ParsableFile(file.getAbsolutePath(),
161: null, null, oses);
162: plist.add(pf);
163: ScriptParser sp = new ScriptParser(plist,
164: new VariableSubstitutor(vars));
165: sp.parseFiles();
166:
167: ArrayList<ExecutableFile> elist = new ArrayList<ExecutableFile>();
168: ExecutableFile ef = new ExecutableFile(file
169: .getAbsolutePath(), ExecutableFile.POSTINSTALL,
170: ExecutableFile.ABORT, oses, false);
171: elist.add(ef);
172: FileExecutor fe = new FileExecutor(elist);
173: int retval = fe.executeFiles(ExecutableFile.POSTINSTALL,
174: this );
175: if (retval == 0) {
176: idata.setVariable("password", pass);
177: isValid = true;
178: }
179: // else is already showing dialog
180: // {
181: // JOptionPane.showMessageDialog(this, "Cannot execute 'sudo' cmd,
182: // check your password", "Error", JOptionPane.ERROR_MESSAGE);
183: // }
184: } catch (Exception e) {
185: // JOptionPane.showMessageDialog(this, "Cannot execute 'sudo' cmd,
186: // check your password", "Error", JOptionPane.ERROR_MESSAGE);
187: e.printStackTrace();
188: isValid = false;
189: }
190: try {
191: if (file != null && file.exists())
192: file.delete();// you don't
193: // want the file
194: // with password
195: // tobe arround,
196: // in case of
197: // error
198: } catch (Exception e) {
199: // ignore
200: }
201: }
202:
203: /**
204: * Indicates wether the panel has been validated or not.
205: *
206: * @return Always true.
207: */
208: public boolean isValidated() {
209: if (!isValid) {
210: doSudoCmd();
211: }
212: if (!isValid) {
213: JOptionPane.showInternalMessageDialog(this , "Password",
214: "Password is not valid", JOptionPane.ERROR_MESSAGE);
215: }
216: return isValid;
217: }
218: }
|