Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8bfb13b186c99e459b83fb3115c6ae34bc35cc1d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*******************************************************************************
 * Copyright (c) 2012 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.linuxtools.internal.ssh.proxy;

import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.window.Window;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;

import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;

public class SSHBase {
    private URI uri;
    private JSch jsch;
    private Session session;
    private ChannelSftp channelSftp;
    private static Map<String, String> passwords = new HashMap<>();

    public SSHBase(URI uri) {
        this.uri = uri;
        jsch=new JSch();
    }

    private Session loadSession() throws CoreException {
        if (session == null || !session.isConnected()) {
            try {
                session=jsch.getSession(uri.getUserInfo(), uri.getHost());
            } catch (JSchException e) {
                throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.SSHBase_CreateSessionFailed + e.getMessage()));
            }

            Properties config = new Properties();
            config.put("StrictHostKeyChecking", "no");  //$NON-NLS-1$,  //$NON-NLS-2$
            session.setConfig(config);

            String password = passwords.get(uri.getAuthority());
            if (password != null) {
                session.setPassword(password);
                try {
                    session.connect();
                    return session;
                } catch (JSchException e) {
                    //Nothing to do. It will try again in next command
                }
            }

            password = askPassword(uri.getUserInfo(), uri.getHost());
            session.setPassword(password);
            try {
                session.connect();
            } catch (JSchException e) {
                throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.SSHBase_CreateSessionFailed + e.getMessage()));
            }
            passwords.put(uri.getAuthority(), password);
        }
        return session;
    }

    private String askPassword(String user, String host) throws CoreException {
        IWorkbenchWindow w = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
        if (w == null) {
            return ""; //$NON-NLS-1$
        }
        SSHPasswordDialog d = new SSHPasswordDialog(w.getShell(), user, host);
        if (d.open() == Window.OK) {
            return d.getPassword();
        } else {
            throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.SSHBase_CreateSessionCancelled));
        }
    }

    protected ChannelSftp getChannelSftp() throws CoreException {
        loadSession();
        if (channelSftp == null || !channelSftp.isConnected()) {
            try {
                channelSftp = (ChannelSftp)session.openChannel("sftp"); //$NON-NLS-1$
                channelSftp.connect();
            } catch (JSchException e) {
                throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.SSHBase_CreateSessionFailed + e.getMessage()));
            }
        }
        return channelSftp;
    }

    protected ChannelExec createChannelExec() throws CoreException {
        loadSession();
        try {
            return (ChannelExec)session.openChannel("exec"); //$NON-NLS-1$
        } catch (JSchException e) {
            throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.SSHBase_CreateSessionFailed + e.getMessage()));
        }
    }
}

Back to the top