Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: bff9274d83341764d473c189cc574516ce3420d8 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*******************************************************************************
 * Copyright (c) 2007, 2008 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Atsuhiko Yamanaka, JCraft,Inc. - adding methods referring to IJSchLocation
 *******************************************************************************/
package org.eclipse.jsch.internal.core;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jsch.core.IJSchService;
import org.eclipse.jsch.core.IJSchLocation;
import org.eclipse.osgi.util.NLS;

import com.jcraft.jsch.*;

/**
 * A static class whose purpose is to ensure that all the JSch preferences are properly pushed into JSch
 * before a {@link Session} is created.
 * @since 1.0
 */
class JSchProvider implements IJSchService {
  
  private static JSchProvider instance;

  /* (non-Javadoc)
   * @see org.eclipse.jsch.core.IJSchService#createSession(java.lang.String, int, java.lang.String)
   */
  public Session createSession(String host, int port, String username) throws JSchException {

    if(JSchCorePlugin.getPlugin().isNeedToLoadKnownHosts()){
      JSchCorePlugin.getPlugin().loadKnownHosts();
    }

    if(JSchCorePlugin.getPlugin().isNeedToLoadKeys()){
      JSchCorePlugin.getPlugin().loadPrivateKeys();
    }
    
    return Utils.createSession(JSchCorePlugin.getPlugin().getJSch(), username, host, port);
  }
  
  /* (non-Javadoc)
   * @see org.eclipse.jsch.core.IJSchService#createSession(IJSchLocation location, UserInfo uinfo)
   */
  public Session createSession(IJSchLocation location, UserInfo uinfo) throws JSchException {

    Session session=createSession(location.getHost(), location.getPort(), location.getUsername());
    
    if(uinfo==null){
      IUserAuthenticator authenticator=getPluggedInAuthenticator();
      if(authenticator==null)
        authenticator=new NullUserAuthenticator();
      uinfo=new UserInfoImpl(location, authenticator, (JSchCorePlugin.getPlugin().getTimeout() * 1000));
    }
    if(uinfo!=null)
      session.setUserInfo(uinfo);
    
    return session;
  }
  
  public Session createSession(IJSchLocation location) throws JSchException {
    return createSession(location, null);
  }
  
  /**
   * @see org.eclipse.jsch.core.IJSchService#connect(com.jcraft.jsch.Session, int, org.eclipse.core.runtime.IProgressMonitor)
   */
  public void connect(Session session, int timeout,
      IProgressMonitor monitor) throws JSchException{
    session.setSocketFactory(new ResponsiveSocketFactory(monitor, timeout));
    
    UserInfo ui=session.getUserInfo();
    
    if(ui!=null && (ui instanceof UserInfoImpl))
      ((UserInfoImpl)ui).aboutToConnect();
    
    try{
      session.connect();
    }
    catch(java.lang.ArrayIndexOutOfBoundsException e){  
      // TODO This catch clause has been added to work around
      // Bug 217980 and will be deleted in the future.
      throw new JSchException("invalid server's version string");//$NON-NLS-1$
    }
    catch(JSchException e){
      
      // Try again since the previous prompt may have obtained
      // the proper credential from the user.
      // This logic had been implemented in org.eclipse.team.internal.ccvs.ssh2.JSchSession#getSession.
      if(isAuthenticationFailure(e)
          &&ui!=null&&(ui instanceof UserInfoImpl)
          &&hasPromptExceededTimeout(session)
          &&((UserInfoImpl)ui).incReuse()==0){
        String host=session.getHost();
        String user=session.getUserName();
        int port=session.getPort();
        session=Utils.createSession(getJSch(), user, host, port);
        session.setUserInfo(ui);
        session.setTimeout(timeout);
        connect(session, timeout, monitor);
        return;
      }
      
      if(session.isConnected())
        session.disconnect();
      throw e;
    }
    
    if(ui!=null && (ui instanceof UserInfoImpl))
      ((UserInfoImpl)ui).connectionMade();
  }
  
  /**
   * @see org.eclipse.jsch.core.IJSchService#getProxyForHost(java.lang.String, java.lang.String)
   */
  public Proxy getProxyForHost(String host, String proxyType) {
    return Utils.getProxyForHost(host, proxyType);
  }

  public static IJSchService getInstance(){
    if (instance == null)
      instance = new JSchProvider();
    return instance;
  }

  /* (non-Javadoc)
   * @see org.eclipse.jsch.core.IJSchService#connect(com.jcraft.jsch.Proxy, java.lang.String, int, int, org.eclipse.core.runtime.IProgressMonitor)
   */
  public void connect(Proxy proxy, String host, int port, int timeout,
      IProgressMonitor monitor) throws JSchException {
    try{
      proxy.connect(new ResponsiveSocketFactory(monitor, timeout), host, port, timeout);
    }
    catch(JSchException e){
      throw e;
    }
    catch(Exception e){
      new JSchException(e.getMessage());
    }
  }
  
  /**
   * Search for an instance of IUserAuthenticator provided from other plug-in.
   * @see org.eclipse.jsch.internal.ui.authenticator.WorkbenchUserAuthenticator
   * @return an instance of IUserAuthenticator.
   */
  private IUserAuthenticator getPluggedInAuthenticator(){
    IExtension[] extensions=Platform.getExtensionRegistry().getExtensionPoint(
        JSchCorePlugin.ID, JSchCorePlugin.PT_AUTHENTICATOR).getExtensions();
    if(extensions.length==0)
      return null;
    IExtension extension=extensions[0];
    IConfigurationElement[] configs=extension.getConfigurationElements();
    if(configs.length==0){
      JSchCorePlugin
          .log(
              IStatus.ERROR,
              NLS
                  .bind(
                      "User autheticator {0} is missing required fields", (new Object[] {extension.getUniqueIdentifier()})), null);//$NON-NLS-1$ 
      return null;
    }
    try{
      IConfigurationElement config=configs[0];
      return (IUserAuthenticator)config.createExecutableExtension("run");//$NON-NLS-1$ 
    }
    catch(CoreException ex){
      JSchCorePlugin
          .log(
              IStatus.ERROR,
              NLS
                  .bind(
                      "Unable to instantiate user authenticator {0}", (new Object[] {extension.getUniqueIdentifier()})), ex);//$NON-NLS-1$ 
      return null;
    }
  }
  
  public JSch getJSch(){
    return JSchCorePlugin.getPlugin().getJSch();
  }

  private boolean isAuthenticationFailure(JSchException ee){
    return ee.getMessage().equals("Auth fail"); //$NON-NLS-1$
  }
  
  private boolean hasPromptExceededTimeout(Session session){
    if(session.getUserInfo()==null || !(session.getUserInfo() instanceof UserInfoImpl))
      return false;
    return ((UserInfoImpl)session.getUserInfo()).hasPromptExceededTimeout();
  }

  /**
   * @see org.eclipse.jsch.core.IJSchService#getLocation(String user, String host, int port)
   */
  public IJSchLocation getLocation(String user, String host, int port){
    IJSchLocation location=null;
    location=new JSchLocation(user, host, port);
    return location;
  }
}

Back to the top