00001 using System;
00002 using System.IO;
00003
00004 namespace Tamir.SharpSsh.jsch
00005 {
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 public class JSch
00036 {
00037
00038 static System.Collections.Hashtable config;
00039
00040 public static void Init()
00041 {
00042 config=new System.Collections.Hashtable();
00043
00044
00045 config.Add("kex", "diffie-hellman-group1-sha1,diffie-hellman-group-exchange-sha1");
00046 config.Add("server_host_key", "ssh-rsa,ssh-dss");
00047
00048
00049
00050
00051
00052 config.Add("cipher.s2c", "3des-cbc,aes128-cbc");
00053 config.Add("cipher.c2s", "3des-cbc,aes128-cbc");
00054
00055
00056
00057 config.Add("mac.s2c", "hmac-md5,hmac-sha1");
00058 config.Add("mac.c2s", "hmac-md5,hmac-sha1");
00059 config.Add("compression.s2c", "none");
00060 config.Add("compression.c2s", "none");
00061 config.Add("lang.s2c", "");
00062 config.Add("lang.c2s", "");
00063
00064 config.Add("diffie-hellman-group-exchange-sha1",
00065 "Tamir.SharpSsh.jsch.DHGEX");
00066 config.Add("diffie-hellman-group1-sha1",
00067 "Tamir.SharpSsh.jsch.DHG1");
00068
00069 config.Add("dh", "Tamir.SharpSsh.jsch.jce.DH");
00070 config.Add("3des-cbc", "Tamir.SharpSsh.jsch.jce.TripleDESCBC");
00071
00072 config.Add("hmac-sha1", "Tamir.SharpSsh.jsch.jce.HMACSHA1");
00073 config.Add("hmac-sha1-96", "Tamir.SharpSsh.jsch.jce.HMACSHA196");
00074 config.Add("hmac-md5", "Tamir.SharpSsh.jsch.jce.HMACMD5");
00075 config.Add("hmac-md5-96", "Tamir.SharpSsh.jsch.jce.HMACMD596");
00076 config.Add("sha-1", "Tamir.SharpSsh.jsch.jce.SHA1");
00077 config.Add("md5", "Tamir.SharpSsh.jsch.jce.MD5");
00078 config.Add("signature.dss", "Tamir.SharpSsh.jsch.jce.SignatureDSA");
00079 config.Add("signature.rsa", "Tamir.SharpSsh.jsch.jce.SignatureRSA");
00080 config.Add("keypairgen.dsa", "Tamir.SharpSsh.jsch.jce.KeyPairGenDSA");
00081 config.Add("keypairgen.rsa", "Tamir.SharpSsh.jsch.jce.KeyPairGenRSA");
00082 config.Add("random", "Tamir.SharpSsh.jsch.jce.Random");
00083
00084 config.Add("aes128-cbc", "Tamir.SharpSsh.jsch.jce.AES128CBC");
00085
00086
00087
00088 config.Add("StrictHostKeyChecking", "ask");
00089 }
00090
00091 internal Tamir.SharpSsh.java.util.Vector pool=new Tamir.SharpSsh.java.util.Vector();
00092 internal Tamir.SharpSsh.java.util.Vector identities=new Tamir.SharpSsh.java.util.Vector();
00093
00094 private HostKeyRepository known_hosts=null;
00095
00096 public JSch()
00097 {
00098
00099 if (config==null)
00100 Init();
00101 }
00102
00103 public Session getSession(String username, String host) { return getSession(username, host, 22); }
00104 public Session getSession(String username, String host, int port)
00105 {
00106 Session s=new Session(this);
00107 s.setUserName(username);
00108 s.setHost(host);
00109 s.setPort(port);
00110 pool.Add(s);
00111 return s;
00112 }
00113
00114 internal bool removeSession(Session session)
00115 {
00116 lock(pool)
00117 {
00118 return pool.remove(session);
00119 }
00120 }
00121
00122 public void setHostKeyRepository(HostKeyRepository foo)
00123 {
00124 known_hosts=foo;
00125 }
00126 public void setKnownHosts(String foo)
00127 {
00128 if(known_hosts==null) known_hosts=new KnownHosts(this);
00129 if(known_hosts is KnownHosts)
00130 {
00131 lock(known_hosts)
00132 {
00133 ((KnownHosts)known_hosts).setKnownHosts(foo);
00134 }
00135 }
00136 }
00137 public void setKnownHosts(StreamReader foo)
00138 {
00139 if(known_hosts==null) known_hosts=new KnownHosts(this);
00140 if(known_hosts is KnownHosts)
00141 {
00142 lock(known_hosts)
00143 {
00144 ((KnownHosts)known_hosts).setKnownHosts(foo);
00145 }
00146 }
00147 }
00148
00149
00150
00151
00152
00153
00154 public HostKeyRepository getHostKeyRepository()
00155 {
00156 if(known_hosts==null) known_hosts=new KnownHosts(this);
00157 return known_hosts;
00158 }
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172 public void addIdentity(String foo)
00173 {
00174 addIdentity(foo, (String)null);
00175 }
00176 public void addIdentity(String foo, String bar)
00177 {
00178 Identity identity=new IdentityFile(foo, this);
00179 if(bar!=null) identity.setPassphrase(bar);
00180 identities.Add(identity);
00181 }
00182 internal String getConfig(String foo){ return (String)(config[foo]); }
00183
00184 private System.Collections.ArrayList proxies;
00185 void setProxy(String hosts, Proxy proxy)
00186 {
00187 String[] patterns=Util.split(hosts, ",");
00188 if(proxies==null){proxies=new System.Collections.ArrayList();}
00189 lock(proxies)
00190 {
00191 for(int i=0; i<patterns.Length; i++)
00192 {
00193 if(proxy==null)
00194 {
00195 proxies[0] = null;
00196 proxies[0] = System.Text.Encoding.Default.GetBytes( patterns[i] );
00197 }
00198 else
00199 {
00200 proxies.Add( System.Text.Encoding.Default.GetBytes( patterns[i] ) );
00201 proxies.Add(proxy);
00202 }
00203 }
00204 }
00205 }
00206 internal Proxy getProxy(String host)
00207 {
00208 if(proxies==null)return null;
00209 byte[] _host= System.Text.Encoding.Default.GetBytes( host );
00210 lock(proxies)
00211 {
00212 for(int i=0; i<proxies.Count; i+=2)
00213 {
00214 if(Util.glob(((byte[])proxies[i]), _host))
00215 {
00216 return (Proxy)(proxies[i+1]);
00217 }
00218 }
00219 }
00220 return null;
00221 }
00222 internal void removeProxy()
00223 {
00224 proxies=null;
00225 }
00226
00227 public static void setConfig(System.Collections.Hashtable foo)
00228 {
00229 lock(config)
00230 {
00231 System.Collections.IEnumerator e=foo.Keys.GetEnumerator();
00232 while(e.MoveNext())
00233 {
00234 String key=(String)(e.Current);
00235 config.Add(key, (String)(foo[key]));
00236 }
00237 }
00238 }
00239 }
00240
00241 }