2
Copyright (c) 2006-2010 ymnk, JCraft,Inc. All rights reserved.
4
Redistribution and use in source and binary forms, with or without
5
modification, are permitted provided that the following conditions are met:
7
1. Redistributions of source code must retain the above copyright notice,
8
this list of conditions and the following disclaimer.
10
2. Redistributions in binary form must reproduce the above copyright
11
notice, this list of conditions and the following disclaimer in
12
the documentation and/or other materials provided with the distribution.
14
3. The names of the authors may not be used to endorse or promote products
15
derived from this software without specific prior written permission.
17
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
18
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
19
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
20
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
21
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
26
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
This code is based on jsch (http://www.jcraft.com/jsch).
29
All credit should go to the authors of jsch.
33
using System.Collections;
34
using System.Collections.Generic;
42
internal static Hashtable config = new Hashtable();
46
// config.put("kex", "diffie-hellman-group-exchange-sha1");
47
config.Put("kex", "diffie-hellman-group1-sha1,diffie-hellman-group-exchange-sha1"
49
config.Put("server_host_key", "ssh-rsa,ssh-dss");
50
// config.put("server_host_key", "ssh-dss,ssh-rsa");
51
config.Put("cipher.s2c", "aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc,aes192-cbc,aes256-cbc"
53
config.Put("cipher.c2s", "aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc,aes192-cbc,aes256-cbc"
55
config.Put("mac.s2c", "hmac-md5,hmac-sha1,hmac-sha1-96,hmac-md5-96");
56
config.Put("mac.c2s", "hmac-md5,hmac-sha1,hmac-sha1-96,hmac-md5-96");
57
config.Put("compression.s2c", "none");
58
// config.put("compression.s2c", "zlib@openssh.com,zlib,none");
59
config.Put("compression.c2s", "none");
60
// config.put("compression.c2s", "zlib@openssh.com,zlib,none");
61
config.Put("lang.s2c", string.Empty);
62
config.Put("lang.c2s", string.Empty);
63
config.Put("compression_level", "6");
64
config.Put("diffie-hellman-group-exchange-sha1", "com.jcraft.jsch.DHGEX");
65
config.Put("diffie-hellman-group1-sha1", "com.jcraft.jsch.DHG1");
66
config.Put("dh", "com.jcraft.jsch.jce.DH");
67
config.Put("3des-cbc", "com.jcraft.jsch.jce.TripleDESCBC");
68
config.Put("blowfish-cbc", "com.jcraft.jsch.jce.BlowfishCBC");
69
config.Put("hmac-sha1", "com.jcraft.jsch.jce.HMACSHA1");
70
config.Put("hmac-sha1-96", "com.jcraft.jsch.jce.HMACSHA196");
71
config.Put("hmac-md5", "com.jcraft.jsch.jce.HMACMD5");
72
config.Put("hmac-md5-96", "com.jcraft.jsch.jce.HMACMD596");
73
config.Put("sha-1", "com.jcraft.jsch.jce.SHA1");
74
config.Put("md5", "com.jcraft.jsch.jce.MD5");
75
config.Put("signature.dss", "com.jcraft.jsch.jce.SignatureDSA");
76
config.Put("signature.rsa", "com.jcraft.jsch.jce.SignatureRSA");
77
config.Put("keypairgen.dsa", "com.jcraft.jsch.jce.KeyPairGenDSA");
78
config.Put("keypairgen.rsa", "com.jcraft.jsch.jce.KeyPairGenRSA");
79
config.Put("random", "com.jcraft.jsch.jce.Random");
80
config.Put("none", "com.jcraft.jsch.CipherNone");
81
config.Put("aes128-cbc", "com.jcraft.jsch.jce.AES128CBC");
82
config.Put("aes192-cbc", "com.jcraft.jsch.jce.AES192CBC");
83
config.Put("aes256-cbc", "com.jcraft.jsch.jce.AES256CBC");
84
config.Put("aes128-ctr", "com.jcraft.jsch.jce.AES128CTR");
85
config.Put("aes192-ctr", "com.jcraft.jsch.jce.AES192CTR");
86
config.Put("aes256-ctr", "com.jcraft.jsch.jce.AES256CTR");
87
config.Put("3des-ctr", "com.jcraft.jsch.jce.TripleDESCTR");
88
config.Put("arcfour", "com.jcraft.jsch.jce.ARCFOUR");
89
config.Put("arcfour128", "com.jcraft.jsch.jce.ARCFOUR128");
90
config.Put("arcfour256", "com.jcraft.jsch.jce.ARCFOUR256");
91
config.Put("userauth.none", "com.jcraft.jsch.UserAuthNone");
92
config.Put("userauth.password", "com.jcraft.jsch.UserAuthPassword");
93
config.Put("userauth.keyboard-interactive", "com.jcraft.jsch.UserAuthKeyboardInteractive"
95
config.Put("userauth.publickey", "com.jcraft.jsch.UserAuthPublicKey");
96
config.Put("userauth.gssapi-with-mic", "com.jcraft.jsch.UserAuthGSSAPIWithMIC");
97
config.Put("gssapi-with-mic.krb5", "com.jcraft.jsch.jgss.GSSContextKrb5");
98
config.Put("zlib", "com.jcraft.jsch.jcraft.Compression");
99
config.Put("zlib@openssh.com", "com.jcraft.jsch.jcraft.Compression");
100
config.Put("StrictHostKeyChecking", "ask");
101
config.Put("HashKnownHosts", "no");
102
//config.put("HashKnownHosts", "yes");
103
config.Put("PreferredAuthentications", "gssapi-with-mic,publickey,keyboard-interactive,password"
105
config.Put("CheckCiphers", "aes256-ctr,aes192-ctr,aes128-ctr,aes256-cbc,aes192-cbc,aes128-cbc,3des-ctr,arcfour,arcfour128,arcfour256"
109
internal ArrayList pool = new ArrayList();
111
internal ArrayList identities = new ArrayList();
113
private HostKeyRepository known_hosts = null;
115
private sealed class _Logger_115 : Logger
121
public override bool IsEnabled(int level)
126
public override void Log(int level, string message)
131
private static readonly Logger DEVNULL = new _Logger_115();
133
internal static Logger logger = DEVNULL;
139
string osname = (string)(Runtime.GetProperties()["os.name"]);
140
if (osname != null && osname.Equals("Mac OS X"))
142
config.Put("hmac-sha1", "com.jcraft.jsch.jcraft.HMACSHA1");
143
config.Put("hmac-md5", "com.jcraft.jsch.jcraft.HMACMD5");
144
config.Put("hmac-md5-96", "com.jcraft.jsch.jcraft.HMACMD596");
145
config.Put("hmac-sha1-96", "com.jcraft.jsch.jcraft.HMACSHA196");
153
/// <exception cref="NSch.JSchException"></exception>
154
public virtual Session GetSession(string username, string host)
156
return GetSession(username, host, 22);
159
/// <exception cref="NSch.JSchException"></exception>
160
public virtual Session GetSession(string username, string host, int port)
162
if (username == null)
164
throw new JSchException("username must not be null.");
168
throw new JSchException("host must not be null.");
170
Session s = new Session(this);
171
s.SetUserName(username);
174
//pool.addElement(s);
178
protected internal virtual void AddSession(Session session)
186
protected internal virtual bool RemoveSession(Session session)
190
return pool.RemoveElement(session);
194
public virtual void SetHostKeyRepository(HostKeyRepository hkrepo)
196
known_hosts = hkrepo;
199
/// <exception cref="NSch.JSchException"></exception>
200
public virtual void SetKnownHosts(string filename)
202
if (known_hosts == null)
204
known_hosts = new KnownHosts(this);
206
if (known_hosts is KnownHosts)
210
((KnownHosts)known_hosts).SetKnownHosts(filename);
215
/// <exception cref="NSch.JSchException"></exception>
216
public virtual void SetKnownHosts(InputStream stream)
218
if (known_hosts == null)
220
known_hosts = new KnownHosts(this);
222
if (known_hosts is KnownHosts)
226
((KnownHosts)known_hosts).SetKnownHosts(stream);
231
public virtual HostKeyRepository GetHostKeyRepository()
233
if (known_hosts == null)
235
known_hosts = new KnownHosts(this);
240
/// <exception cref="NSch.JSchException"></exception>
241
public virtual void AddIdentity(string prvkey)
243
AddIdentity(prvkey, (byte[])null);
246
/// <exception cref="NSch.JSchException"></exception>
247
public virtual void AddIdentity(string prvkey, string passphrase)
249
byte[] _passphrase = null;
250
if (passphrase != null)
252
_passphrase = Util.Str2byte(passphrase);
254
AddIdentity(prvkey, _passphrase);
255
if (_passphrase != null)
257
Util.Bzero(_passphrase);
261
/// <exception cref="NSch.JSchException"></exception>
262
public virtual void AddIdentity(string prvkey, byte[] passphrase)
264
Identity identity = IdentityFile.NewInstance(prvkey, null, this);
265
AddIdentity(identity, passphrase);
268
/// <exception cref="NSch.JSchException"></exception>
269
public virtual void AddIdentity(string prvkey, string pubkey, byte[] passphrase)
271
Identity identity = IdentityFile.NewInstance(prvkey, pubkey, this);
272
AddIdentity(identity, passphrase);
275
/// <exception cref="NSch.JSchException"></exception>
276
public virtual void AddIdentity(string name, byte[] prvkey, byte[] pubkey, byte[]
279
Identity identity = IdentityFile.NewInstance(name, prvkey, pubkey, this);
280
AddIdentity(identity, passphrase);
283
/// <exception cref="NSch.JSchException"></exception>
284
public virtual void AddIdentity(Identity identity, byte[] passphrase)
286
if (passphrase != null)
290
byte[] goo = new byte[passphrase.Length];
291
System.Array.Copy(passphrase, 0, goo, 0, passphrase.Length);
293
identity.SetPassphrase(passphrase);
297
Util.Bzero(passphrase);
302
if (!identities.Contains(identity))
304
identities.Add(identity);
309
/// <exception cref="NSch.JSchException"></exception>
310
public virtual void RemoveIdentity(string name)
314
for (int i = 0; i < identities.Count; i++)
316
Identity identity = (Identity)(identities[i]);
317
if (!identity.GetName().Equals(name))
321
identities.RemoveElement(identity);
328
/// <exception cref="NSch.JSchException"></exception>
329
public virtual ArrayList GetIdentityNames()
331
ArrayList foo = new ArrayList();
334
for (int i = 0; i < identities.Count; i++)
336
Identity identity = (Identity)(identities[i]);
337
foo.Add(identity.GetName());
343
/// <exception cref="NSch.JSchException"></exception>
344
public virtual void RemoveAllIdentity()
348
ArrayList foo = GetIdentityNames();
349
for (int i = 0; i < foo.Count; i++)
351
string name = ((string)foo[i]);
352
RemoveIdentity(name);
357
public static string GetConfig(string key)
361
string s = (string)(config[key]);
362
s = s.Replace ("com.jcraft.jsch.jce","NSch.Jce");
363
s = s.Replace ("com.jcraft.jsch.jcraft","NSch.Jcraft");
364
s = s.Replace ("com.jcraft.jsch.jgss","NSch.Jgss");
365
s = s.Replace ("com.jcraft.jsch","NSch");
370
public static void SetConfig(Hashtable newconf)
374
for (IEnumerator e = newconf.Keys.GetEnumerator (); e.MoveNext(); )
376
string key = (string)(e.Current);
377
config.Put(key, (string)(newconf[key]));
382
public static void SetConfig(string key, string value)
384
config.Put(key, value);
387
public static void SetLogger(Logger logger)
391
NSch.JSch.logger = DEVNULL;
393
NSch.JSch.logger = logger;
396
internal static Logger GetLogger()