001: /*
002: * This program is free software; you can redistribute it and/or
003: * modify it under the terms of the GNU General Public License
004: * as published by the Free Software Foundation; either version 2
005: * of the License, or (at your option) any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU General Public License for more details.
011:
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
015: */
016: package net.sf.jftp.gui.hostchooser;
017:
018: import java.awt.Cursor;
019: import java.awt.GridLayout;
020: import java.awt.Insets;
021: import java.awt.event.ActionEvent;
022: import java.awt.event.ActionListener;
023: import java.awt.event.ComponentEvent;
024: import java.awt.event.ComponentListener;
025: import java.awt.event.WindowEvent;
026: import java.awt.event.WindowListener;
027: import java.io.File;
028: import java.io.IOException;
029:
030: import javax.swing.JCheckBox;
031: import javax.swing.JComboBox;
032: import javax.swing.JDialog;
033: import javax.swing.JFileChooser;
034: import javax.swing.JLabel;
035: import javax.swing.event.ChangeEvent;
036: import javax.swing.event.ChangeListener;
037:
038: import net.sf.jftp.JFtp;
039: import net.sf.jftp.config.LoadSet;
040: import net.sf.jftp.config.SaveSet;
041: import net.sf.jftp.config.Settings;
042: import net.sf.jftp.gui.framework.HButton;
043: import net.sf.jftp.gui.framework.HFrame;
044: import net.sf.jftp.gui.framework.HPanel;
045: import net.sf.jftp.gui.framework.HPasswordField;
046: import net.sf.jftp.gui.framework.HTextField;
047: import net.sf.jftp.net.Sftp2Connection;
048: import net.sf.jftp.net.SftpConnection;
049: import net.sf.jftp.net.StartConnection;
050: import net.sf.jftp.system.logging.Log;
051: import net.sf.jftp.tools.SshShell;
052:
053: public class SftpHostChooser extends HFrame implements ActionListener,
054: WindowListener, ChangeListener {
055: public HTextField host = new HTextField("Host:", "localhost");
056: public HTextField user = new HTextField("Username:", "guest");
057: public HTextField port = new HTextField("Port:", "22");
058: public HPasswordField pass = new HPasswordField("Password/Phrase:",
059: "nopasswd");
060: public JComboBox enc = new JComboBox();
061: public JComboBox cs = new JComboBox();
062: public JComboBox keys = new JComboBox();
063: public JLabel encL = new JLabel("Pref. Encryption");
064: public JLabel csL = new JLabel("Pref. Message Auth.");
065: public JLabel keysL = new JLabel("Pref. Public Key");
066: public JLabel keyfileL = new JLabel("(No File)");
067: private HPanel okP = new HPanel();
068: private HPanel keyP = new HPanel();
069: private HButton ok = new HButton("Connect");
070: private HButton keyfile = new HButton("Choose Key File");
071: private ComponentListener listener = null;
072: private boolean useLocal = false;
073: private boolean shell = false;
074: private String keyfileName = null;
075: private JCheckBox useJSch = new JCheckBox(
076: "Use JSch instead of j2ssh");
077:
078: public SftpHostChooser(ComponentListener l, boolean local) {
079: listener = l;
080: useLocal = local;
081: init();
082: }
083:
084: public SftpHostChooser(ComponentListener l) {
085: listener = l;
086: init();
087: }
088:
089: public SftpHostChooser() {
090: init();
091: }
092:
093: public SftpHostChooser(boolean shell) {
094: this .shell = shell;
095: init();
096: }
097:
098: public void init() {
099: //setSize(500, 200);
100: setLocation(100, 150);
101: setTitle("Sftp Connection...");
102: setBackground(okP.getBackground());
103: getContentPane().setLayout(new GridLayout(7, 2, 5, 3));
104:
105: //***MY CHANGES
106: try {
107: File f = new File(Settings.appHomeDir);
108: f.mkdir();
109:
110: File f1 = new File(Settings.login);
111: f1.createNewFile();
112:
113: File f2 = new File(Settings.login_def_sftp);
114: f2.createNewFile();
115: } catch (IOException ex) {
116: ex.printStackTrace();
117: }
118:
119: String[] login = LoadSet.loadSet(Settings.login_def_sftp);
120:
121: if ((login[0] != null) && (login.length > 1)) {
122: host.setText(login[0]);
123: user.setText(login[1]);
124: }
125:
126: /*
127: else {
128: host.setText("localhost");
129: user.setText("guest");
130:
131: }
132: */
133: if (Settings.getStorePasswords()) {
134: if ((login != null) && (login.length > 2)
135: && (login[2] != null)) {
136: pass.setText(login[2]);
137: }
138: } else {
139: pass.setText("");
140: }
141:
142: enc.addItem("3des-cbc");
143: enc.addItem("blowfish-cbc");
144:
145: cs.addItem("hmac-sha1");
146: cs.addItem("hmac-sha1-96");
147: cs.addItem("hmac-md5");
148: cs.addItem("hmac-md5-96");
149:
150: keys.addItem("ssh-rsa");
151: keys.addItem("ssh-dss");
152:
153: //***end of my changes (for this section)
154: getContentPane().add(host);
155: getContentPane().add(port);
156: getContentPane().add(user);
157: getContentPane().add(pass);
158: getContentPane().add(encL);
159: getContentPane().add(enc);
160: getContentPane().add(csL);
161: getContentPane().add(cs);
162: getContentPane().add(keysL);
163: getContentPane().add(keys);
164: getContentPane().add(keyP);
165: getContentPane()
166: .add(
167: new JLabel(
168: "Keyfiles are usually located ~/.ssh/ on UNIX"));
169: getContentPane().add(useJSch);
170: getContentPane().add(okP);
171:
172: keyP.add(keyfileL);
173: keyP.add(keyfile);
174: okP.add(new JLabel(" "));
175: okP.add(ok);
176: ok.addActionListener(this );
177: keyfile.addActionListener(this );
178:
179: setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
180:
181: pass.text.addActionListener(this );
182:
183: useJSch.addChangeListener(this );
184:
185: pack();
186: setModal(false);
187: setVisible(false);
188: addWindowListener(this );
189: }
190:
191: public void stateChanged(ChangeEvent e) {
192: if (useJSch.isSelected()) {
193: enc.setEnabled(false);
194: cs.setEnabled(false);
195: keys.setEnabled(false);
196: } else {
197: keyfile.setEnabled(true);
198: enc.setEnabled(true);
199: cs.setEnabled(true);
200: keys.setEnabled(true);
201: }
202: }
203:
204: public void update() {
205: fixLocation();
206: setVisible(true);
207: toFront();
208: host.requestFocus();
209: }
210:
211: public void setShell(boolean shell) {
212: this .shell = shell;
213: }
214:
215: public boolean getShell() {
216: return shell;
217: }
218:
219: public void actionPerformed(ActionEvent e) {
220: if ((e.getSource() == ok) || (e.getSource() == pass.text)) {
221: // Switch windows
222: //this.setVisible(false);
223: //this.setModal(false);
224: //JFtp.mainFrame.setVisible(true);
225: //JFtp.mainFrame.toFront();
226: setCursor(new Cursor(Cursor.WAIT_CURSOR));
227:
228: SftpConnection con = null;
229:
230: String htmp = host.getText().trim();
231: String utmp = user.getText().trim();
232: String ptmp = pass.getText();
233:
234: //***port number: to be initialized in a future version?
235: int potmp = 22;
236:
237: try {
238: potmp = Integer.parseInt(port.getText());
239: } catch (Exception ex) {
240: Log.debug("Error: Not a number!");
241: }
242:
243: String potmpString = new String("" + potmp);
244:
245: com.sshtools.j2ssh.configuration.SshConnectionProperties properties = new com.sshtools.j2ssh.configuration.SshConnectionProperties();
246: properties.setHost(htmp);
247: //Log.debug(htmp+":"+properties.getHost());
248: properties.setPort(potmp);
249: properties.setPrefSCEncryption((String) enc
250: .getSelectedItem());
251: properties.setPrefCSMac((String) cs.getSelectedItem());
252: properties
253: .setPrefPublicKey((String) keys.getSelectedItem());
254:
255: if (shell) {
256: SshShell s = new SshShell(properties, utmp, ptmp, potmp);
257: setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
258: this .dispose();
259: s.toFront();
260:
261: return;
262: } else {
263: try {
264: boolean status;
265:
266: SaveSet s = new SaveSet(Settings.login_def_sftp,
267: htmp, utmp, ptmp, potmpString, "null",
268: "null");
269:
270: if (!useJSch.isSelected()) {
271: StartConnection.setSshProperties(properties);
272: StartConnection.setSshKeyfile(keyfileName);
273: status = StartConnection.startCon("SFTP", htmp,
274: utmp, ptmp, potmp, "", useLocal);
275: } else {
276: Sftp2Connection con2 = new Sftp2Connection(
277: htmp, "" + potmp, keyfileName);
278:
279: if (con2.login(utmp, ptmp)) {
280: if (useLocal) {
281: JFtp.statusP.jftp.addLocalConnection(
282: htmp, con2);
283: } else {
284: JFtp.statusP.jftp.addConnection(htmp,
285: con2);
286: }
287:
288: if (con2.chdir(con2.getPWD())
289: || con2.chdir("/")) {
290: ;
291: }
292: }
293: }
294: } catch (Exception ex) {
295: ex.printStackTrace();
296: Log
297: .debug("Could not create SftpConnection, does this distribution come with j2ssh?");
298: }
299: }
300:
301: setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
302: this .dispose();
303: JFtp.mainFrame.setVisible(true);
304: JFtp.mainFrame.toFront();
305:
306: if (listener != null) {
307: listener.componentResized(new ComponentEvent(this , 0));
308: }
309: } else if (e.getSource() == keyfile) {
310: JFileChooser chooser = new JFileChooser();
311: int returnVal = chooser.showOpenDialog(this );
312:
313: if (returnVal == JFileChooser.APPROVE_OPTION) {
314: keyfileName = chooser.getSelectedFile().getPath();
315:
316: if (keyfileName != null) {
317: keyfileL.setText("(File present)");
318: }
319: } else {
320: keyfileName = null;
321:
322: if (keyfileName != null) {
323: keyfileL.setText("(No File)");
324: }
325: }
326: }
327: }
328:
329: public void windowClosing(WindowEvent e) {
330: //System.exit(0);
331: this .dispose();
332: }
333:
334: public void windowClosed(WindowEvent e) {
335: }
336:
337: public void windowActivated(WindowEvent e) {
338: }
339:
340: public void windowDeactivated(WindowEvent e) {
341: }
342:
343: public void windowIconified(WindowEvent e) {
344: }
345:
346: public void windowDeiconified(WindowEvent e) {
347: }
348:
349: public void windowOpened(WindowEvent e) {
350: }
351:
352: public Insets getInsets() {
353: Insets std = super .getInsets();
354:
355: return new Insets(std.top + 10, std.left + 10, std.bottom + 10,
356: std.right + 10);
357: }
358:
359: public void pause(int time) {
360: try {
361: Thread.sleep(time);
362: } catch (Exception ex) {
363: }
364: }
365: }
|