Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d7c9ee90d5244a171d2e8a294193baef120c9327 (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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/****************************************************************************
 * Copyright (c) 2007 Composent, Inc. 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:
 *    Composent, Inc. - initial API and implementation
 *****************************************************************************/

package org.eclipse.ecf.internal.provider.filetransfer.scp;

import com.jcraft.jsch.*;
import java.io.*;
import java.net.URL;
import java.util.Map;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.ecf.core.security.*;
import org.eclipse.ecf.core.util.Proxy;
import org.eclipse.osgi.util.NLS;

/**
 *
 */
public class ScpUtil implements UserInfo, UIKeyboardInteractive {

	public static final String SCP_SSHHOMEDIRECTORY = System
			.getProperty(
					"org.eclipse.ecf.filetransfer.scp.util.sshHomeDirectory", "sshHomeDirectory"); //$NON-NLS-1$
	public static final String SCP_PUBLICKEYFILE = System.getProperty(
			"org.eclipse.ecf.filetransfer.scp.util.keyFile", "keyFile"); //$NON-NLS-1$
	public static final String SCP_KNOWNHOSTSFILE = System
			.getProperty(
					"org.eclipse.ecf.filetransfer.scp.util.knownHostsFile", "knownHostsFile"); //$NON-NLS-1$

	public static final int DEFAULT_SCP_PORT = Integer
			.parseInt(System.getProperty(
					"org.eclipse.ecf.filetransfer.scp.util.scpPort", "22"));

	private IScpFileTransfer handler;
	private String password;
	private String passphrase;
	private Session session;

	private String sshHome = null;
	private String keyFile = null;
	private String knownHostsFile = null;

	public ScpUtil(IScpFileTransfer handler) throws JSchException, IOException,
			UnsupportedCallbackException {
		this.handler = handler;
		final JSch jsch = new JSch();
		final URL url = handler.getTargetURL();
		int port = url.getPort();
		if (port == -1)
			port = DEFAULT_SCP_PORT;
		setupOptions(jsch);
		promptUsername();
		String username = handler.getUsername();
		if (username == null)
			throw new IOException(Messages.ScpUtil_EXCEPTION_USERNAME_NOT_NULL);
		session = jsch.getSession(handler.getUsername(), url.getHost(), port);
		setupProxy();
		session.setUserInfo(this);
	}

	Session getSession() {
		return session;
	}

	void promptUsername() throws IOException, UnsupportedCallbackException {
		final IConnectContext connectContext = handler.getConnectContext();
		if (connectContext != null) {
			final CallbackHandler callbackHandler = connectContext
					.getCallbackHandler();
			if (handler != null) {
				final Callback[] callbacks = new Callback[2];
				final NameCallback nc = new NameCallback(
						Messages.ScpOutgoingFileTransfer_USERNAME_PROMPT);
				String user = handler.getUsername();
				if (user != null)
					nc.setName(user);
				callbacks[0] = nc;
				callbacks[1] = new PasswordCallback(
						Messages.ScpOutgoingFileTransfer_PASSWORD_PROMPT);
				callbackHandler.handle(callbacks);
				handler.setUsername(nc.getName());
			}
		}
	}

	String promptCredentials(boolean usePassphrase) {
		try {
			final IConnectContext connectContext = handler.getConnectContext();
			if (connectContext != null) {
				final CallbackHandler callbackHandler = connectContext
						.getCallbackHandler();
				if (handler != null) {
					final Callback[] callbacks = new Callback[2];
					final NameCallback nc = new NameCallback(
							Messages.ScpOutgoingFileTransfer_USERNAME_PROMPT);
					String user = handler.getUsername();
					if (user != null)
						nc.setName(user);
					callbacks[0] = nc;
					if (usePassphrase) {
						callbacks[1] = new PassphraseCallback(
								Messages.ScpOutgoingFileTransfer_PASSPHRASE_PROMPT);
					} else
						callbacks[1] = new PasswordCallback(
								Messages.ScpOutgoingFileTransfer_PASSWORD_PROMPT);
					callbackHandler.handle(callbacks);
					handler.setUsername(nc.getName());
					if (usePassphrase) {
						passphrase = ((PassphraseCallback) callbacks[1])
								.getPassphrase();
					} else
						password = ((PasswordCallback) callbacks[1])
								.getPassword();
				}
			}
			return (usePassphrase) ? this.passphrase : this.password;
		} catch (final Exception e) {
			return null;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.jcraft.jsch.UserInfo#getPassphrase()
	 */
	public String getPassphrase() {
		return promptCredentials(true);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.jcraft.jsch.UserInfo#getPassword()
	 */
	public String getPassword() {
		return promptCredentials(false);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.jcraft.jsch.UserInfo#promptPassphrase(java.lang.String)
	 */
	public boolean promptPassphrase(String message) {
		return (keyFile != null);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.jcraft.jsch.UserInfo#promptPassword(java.lang.String)
	 */
	public boolean promptPassword(String message) {
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.jcraft.jsch.UserInfo#promptYesNo(java.lang.String)
	 */
	public boolean promptYesNo(String message) {
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.jcraft.jsch.UserInfo#showMessage(java.lang.String)
	 */
	public void showMessage(String message) {
		// do nothing
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.jcraft.jsch.UIKeyboardInteractive#promptKeyboardInteractive(java.
	 * lang.String, java.lang.String, java.lang.String, java.lang.String[],
	 * boolean[])
	 */
	public String[] promptKeyboardInteractive(String destination, String name,
			String instruction, String[] prompt, boolean[] echo) {
		promptCredentials(false);
		return new String[] { password };
	}

	/**
	 */
	void setupProxy() {
		com.jcraft.jsch.Proxy jProxy = null;
		final Proxy proxy = handler.getProxy();
		if (proxy != null) {
			final String hostname = proxy.getAddress().getHostName();
			final int port = proxy.getAddress().getPort();
			if (proxy.getType().equals(Proxy.Type.HTTP)) {
				if (port == -1)
					jProxy = new ProxyHTTP(hostname);
				else
					jProxy = new ProxyHTTP(hostname, port);
			} else if (proxy.getType().equals(Proxy.Type.SOCKS)) {
				if (port == -1)
					jProxy = new ProxySOCKS5(hostname);
				else
					jProxy = new ProxySOCKS5(hostname, port);
			}
			if (jProxy != null)
				session.setProxy(jProxy);
		}
	}

	private void setupOptions(JSch jsch) {
		// Get sshHome
		sshHome = getProperty(SCP_SSHHOMEDIRECTORY, sshHome);
		if (sshHome == null) {
			final String userHome = System.getProperty("user.home"); //$NON-NLS-1$
			if (userHome != null) {
				File dir = new File(userHome + File.separator + ".ssh"); //$NON-NLS-1$
				if (dir.exists())
					sshHome = dir.getAbsolutePath();
				else
					dir = new File(userHome + File.separator + "ssh"); //$NON-NLS-1$
				if (dir.exists())
					sshHome = dir.getAbsolutePath();
			}
		}
		keyFile = getProperty(SCP_PUBLICKEYFILE);
		if (keyFile != null) {
			if (!(new File(keyFile).exists()))
				keyFile = null;
		} else {
			File file = new File(sshHome + File.separator + "id_dsa"); //$NON-NLS-1$
			if (file.exists())
				keyFile = file.getAbsolutePath();
			else {
				file = new File(sshHome + File.separator + "id_rsa"); //$NON-NLS-1$
				if (file.exists())
					keyFile = file.getAbsolutePath();
			}
		}
		if (keyFile != null) {
			try {
				jsch.addIdentity(keyFile);
			} catch (final JSchException e) {
				Activator
						.getDefault()
						.log(new Status(
								IStatus.ERROR,
								Activator.PLUGIN_ID,
								IStatus.ERROR,
								Messages.ScpOutgoingFileTransfer_EXCEPTION_SETTING_SSH_IDENTITY,
								e));
			}
		}
		knownHostsFile = getProperty(SCP_KNOWNHOSTSFILE);
		if (knownHostsFile != null) {
			if (!(new File(knownHostsFile).exists()))
				knownHostsFile = null;
		} else {
			final File file = new File(sshHome + File.separator + "known_hosts"); //$NON-NLS-1$
			if (!file.exists()) {
				knownHostsFile = null;
			} else {
				knownHostsFile = file.getAbsolutePath();
			}
		}
		if (knownHostsFile != null) {
			try {
				jsch.setKnownHosts(knownHostsFile);
			} catch (final JSchException e) {
				Activator
						.getDefault()
						.log(new Status(
								IStatus.ERROR,
								Activator.PLUGIN_ID,
								IStatus.ERROR,
								Messages.ScpOutgoingFileTransfer_EXCEPTION_SETTING_KNOWN_HOSTS,
								e));
			}
		}
	}

	private String getProperty(String key, String def) {
		final Map options = handler.getOptions();
		if (options == null)
			return def;
		final String result = (String) options.get(key);
		if (result == null)
			return def;
		return result;
	}

	private String getProperty(String key) {
		return getProperty(key, null);
	}

	String trimTargetFile(String string) {
		return string;
	}

	void checkAck(InputStream ins) throws IOException {
		checkAck(ins.read(), ins);
	}

	void checkAck(int b, InputStream ins) throws IOException {
		if (b == 0)
			return;
		if (b == -1)
			throw new IOException(Messages.ScpUtil_EXCEPTION_UNKNOWN_SCP_ERROR);

		if (b == 1 || b == 2) {
			final StringBuffer sb = new StringBuffer();
			int c;
			do {
				c = ins.read();
				sb.append((char) c);
			} while (c != '\n');
			if (b == 1) { // error
				throw new IOException(NLS.bind(Messages.ScpUtil_SCP_ERROR,
						sb.toString()));
			}
			if (b == 2) { // fatal error
				throw new IOException(NLS.bind(Messages.ScpUtil_SCP_ERROR,
						sb.toString()));
			}
		}

	}

	void sendZeroToStream(OutputStream outs) throws IOException {
		// send '\0'
		final byte[] buf = new byte[1];
		buf[0] = 0;
		outs.write(buf, 0, 1);
		outs.flush();
	}

	void dispose() {
		if (session != null) {
			session.disconnect();
			session = null;
		}
		handler = null;
		password = null;
		passphrase = null;
		sshHome = null;
		keyFile = null;
		knownHostsFile = null;
	}
}

Back to the top