Statistics
| Revision:

root / import / ext3D / trunk / install-extension3d / IzPack / src / lib / com / izforge / izpack / panels / SudoPanel.java @ 15280

History | View | Annotate | Download (5.73 KB)

1
/*
2
 *  $Id: SudoPanel.java,v 1.1 2006/06/14 07:29:07 cesar Exp $
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

    
26
package com.izforge.izpack.panels;
27

    
28
import com.izforge.izpack.*;
29
import com.izforge.izpack.gui.LabelFactory;
30
import com.izforge.izpack.installer.*;
31
import com.izforge.izpack.util.FileExecutor;
32
import com.izforge.izpack.util.OsConstraint;
33

    
34
import java.awt.BorderLayout;
35
import java.awt.Dimension;
36
import java.awt.event.*;
37
import java.io.File;
38
import java.io.FileOutputStream;
39

    
40
import java.util.*;
41

    
42
import javax.swing.*;
43

    
44
/**
45
 *  The packs selection panel class.
46
 *
47
 * @author     Jan Blok
48
 * @since      November 27, 2003
49
 */
50
public class SudoPanel extends IzPanel implements ActionListener
51
{
52
        private JTextField passwordField;
53
        private boolean isValid = false;
54
        
55
        /**
56
         *  The constructor.
57
         *
58
         * @param  parent  The parent window.
59
         * @param  idata   The installation data.
60
         */
61
        public SudoPanel(InstallerFrame parent, InstallData idata)
62
        {
63
                super(parent, idata);
64

    
65
                setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
66

    
67
                add(LabelFactory.create(
68
                                /*parent.langpack.getString("SudoPanel.info")*/"For installing administrator privileges are necessary",
69
                                JLabel.TRAILING));
70

    
71
                add(Box.createRigidArea(new Dimension(0, 5)));
72
                
73
                add(LabelFactory.create(
74
                                /*parent.langpack.getString("SudoPanel.tip")*/"Please note that passwords are case-sensitive", parent.icons.getImageIcon("tip"),
75
                                JLabel.TRAILING));
76

    
77
                add(Box.createRigidArea(new Dimension(0, 5)));
78

    
79
                JPanel spacePanel = new JPanel();
80
                spacePanel.setAlignmentX(LEFT_ALIGNMENT);
81
                spacePanel.setAlignmentY(CENTER_ALIGNMENT);
82
                spacePanel.setBorder(BorderFactory.createEmptyBorder(80, 30, 0, 50));
83
                spacePanel.setLayout(new BorderLayout(5,5));
84
                spacePanel.add(
85
    LabelFactory.create(
86
                                /*parent.langpack.getString("SudoPanel.specifyAdminPassword")*/"Please specify your password:"),BorderLayout.NORTH);
87
                passwordField = new JPasswordField();
88
                passwordField.addActionListener(this);
89
                JPanel space2Panel = new JPanel();
90
                space2Panel.setLayout(new BorderLayout());
91
                space2Panel.add(passwordField,BorderLayout.NORTH);
92
                space2Panel.add(Box.createRigidArea(new Dimension(0, 5)),BorderLayout.CENTER);
93
                spacePanel.add(space2Panel,BorderLayout.CENTER);
94
                add(spacePanel);
95
        }
96

    
97
        /**  Called when the panel becomes active.  */
98
        public void panelActivate()
99
        {
100
                passwordField.requestFocus();
101
        }
102

    
103
        /**
104
         *  Actions-handling method.
105
         *
106
         * @param  e  The event.
107
         */
108
        public void actionPerformed(ActionEvent e)
109
        {
110
                doSudoCmd();
111
        }
112

    
113
        //check if sudo password is correct (so sudo can be used in all other scripts, even without password, lasts for 5 minutes)
114
        private void doSudoCmd()
115
        {
116
                String pass = passwordField.getText();
117
                
118
                File file = null;
119
                try
120
                {
121
                        //write file in /tmp
122
                        file = new File("/tmp/cmd_sudo.sh");//""c:/temp/run.bat""
123
                        FileOutputStream fos = new FileOutputStream(file);
124
                        fos.write("echo $password | sudo -S ls\nexit $?".getBytes()); //"echo $password > pipo.txt"
125
                        fos.close();
126
                        
127
                        //execute
128
                        HashMap vars = new HashMap();
129
                        vars.put("password", pass);
130

    
131
                        List oses = new ArrayList();
132
                        oses.add(new OsConstraint("unix",null,null,null));
133
                        
134
                        ArrayList plist = new ArrayList();
135
                        ParsableFile pf = new ParsableFile(file.getAbsolutePath(),null,null,oses);
136
                        plist.add(pf);
137
                        ScriptParser sp = new ScriptParser(plist,new VariableSubstitutor(vars));
138
                        sp.parseFiles();
139
                        
140
                        ArrayList elist = new ArrayList();
141
                        ExecutableFile ef = new ExecutableFile(file.getAbsolutePath(),ExecutableFile.POSTINSTALL,ExecutableFile.ABORT,oses,false);
142
                        elist.add(ef);
143
                        FileExecutor fe = new FileExecutor(elist);
144
                        int retval = fe.executeFiles(ExecutableFile.POSTINSTALL,this);
145
                        if (retval == 0)
146
                        {
147
                                idata.setVariable("password", pass);
148
                                isValid = true;
149
                        }
150
//                        else is already showing dialog
151
//                        {
152
//                                JOptionPane.showMessageDialog(this, "Cannot execute 'sudo' cmd, check your password", "Error", JOptionPane.ERROR_MESSAGE);
153
//                        }
154
                }
155
                catch (Exception e)
156
                {
157
//                                JOptionPane.showMessageDialog(this, "Cannot execute 'sudo' cmd, check your password", "Error", JOptionPane.ERROR_MESSAGE);
158
                        e.printStackTrace();
159
                        isValid = false;
160
                }
161
                try
162
                {
163
                        if (file != null && file.exists()) file.delete();//you don't want the file with password tobe arround, in case of error
164
                }
165
                catch (Exception e)
166
                {
167
                        //ignore
168
                }
169
        }
170

    
171
        /**
172
         *  Indicates wether the panel has been validated or not.
173
         *
174
         * @return    Always true.
175
         */
176
        public boolean isValidated()
177
        {
178
                if (!isValid)
179
                {
180
                        doSudoCmd();
181
                }
182
                if (!isValid)
183
                {
184
                        JOptionPane.showInternalMessageDialog(this, "Password", "Password is not valid", JOptionPane.ERROR_MESSAGE);
185
                }
186
                return isValid;
187
        }
188
}