KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcraft > jsch > JSch


1 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
2 /*
3 Copyright (c) 2002,2003,2004,2005,2006 ymnk, JCraft,Inc. All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8   1. Redistributions of source code must retain the above copyright notice,
9      this list of conditions and the following disclaimer.
10
11   2. Redistributions in binary form must reproduce the above copyright
12      notice, this list of conditions and the following disclaimer in
13      the documentation and/or other materials provided with the distribution.
14
15   3. The names of the authors may not be used to endorse or promote products
16      derived from this software without specific prior written permission.
17
18 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
19 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
21 INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
22 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */

29
30 package com.jcraft.jsch;
31
32 import java.io.InputStream JavaDoc;
33 import java.util.Vector JavaDoc;
34
35 public class JSch{
36   static java.util.Hashtable JavaDoc config=new java.util.Hashtable JavaDoc();
37   static{
38 // config.put("kex", "diffie-hellman-group-exchange-sha1");
39
config.put("kex", "diffie-hellman-group1-sha1,diffie-hellman-group-exchange-sha1");
40     config.put("server_host_key", "ssh-rsa,ssh-dss");
41 // config.put("server_host_key", "ssh-dss,ssh-rsa");
42

43     config.put("cipher.s2c", "3des-cbc,blowfish-cbc");
44     config.put("cipher.c2s", "3des-cbc,blowfish-cbc");
45
46     config.put("mac.s2c", "hmac-md5,hmac-sha1,hmac-sha1-96,hmac-md5-96");
47     config.put("mac.c2s", "hmac-md5,hmac-sha1,hmac-sha1-96,hmac-md5-96");
48     config.put("compression.s2c", "none");
49     // config.put("compression.s2c", "zlib@openssh.com,zlib,none");
50
config.put("compression.c2s", "none");
51     // config.put("compression.c2s", "zlib@openssh.com,zlib,none");
52

53     config.put("lang.s2c", "");
54     config.put("lang.c2s", "");
55
56     config.put("compression_level", "6");
57
58     config.put("diffie-hellman-group-exchange-sha1",
59                                 "com.jcraft.jsch.DHGEX");
60     config.put("diffie-hellman-group1-sha1",
61                             "com.jcraft.jsch.DHG1");
62
63     config.put("dh", "com.jcraft.jsch.jce.DH");
64     config.put("3des-cbc", "com.jcraft.jsch.jce.TripleDESCBC");
65     config.put("blowfish-cbc", "com.jcraft.jsch.jce.BlowfishCBC");
66     config.put("hmac-sha1", "com.jcraft.jsch.jce.HMACSHA1");
67     config.put("hmac-sha1-96", "com.jcraft.jsch.jce.HMACSHA196");
68     config.put("hmac-md5", "com.jcraft.jsch.jce.HMACMD5");
69     config.put("hmac-md5-96", "com.jcraft.jsch.jce.HMACMD596");
70     config.put("sha-1", "com.jcraft.jsch.jce.SHA1");
71     config.put("md5", "com.jcraft.jsch.jce.MD5");
72     config.put("signature.dss", "com.jcraft.jsch.jce.SignatureDSA");
73     config.put("signature.rsa", "com.jcraft.jsch.jce.SignatureRSA");
74     config.put("keypairgen.dsa", "com.jcraft.jsch.jce.KeyPairGenDSA");
75     config.put("keypairgen.rsa", "com.jcraft.jsch.jce.KeyPairGenRSA");
76     config.put("random", "com.jcraft.jsch.jce.Random");
77
78     config.put("none", "com.jcraft.jsch.CipherNone");
79
80     config.put("aes128-cbc", "com.jcraft.jsch.jce.AES128CBC");
81     config.put("aes192-cbc", "com.jcraft.jsch.jce.AES192CBC");
82     config.put("aes256-cbc", "com.jcraft.jsch.jce.AES256CBC");
83 // config.put("cipher.s2c", "aes128-cbc,3des-cbc,blowfish-cbc");
84
// config.put("cipher.c2s", "aes128-cbc,3des-cbc,blowfish-cbc");
85

86     config.put("userauth.none", "com.jcraft.jsch.UserAuthNone");
87     config.put("userauth.password", "com.jcraft.jsch.UserAuthPassword");
88     config.put("userauth.keyboard-interactive", "com.jcraft.jsch.UserAuthKeyboardInteractive");
89     config.put("userauth.publickey", "com.jcraft.jsch.UserAuthPublicKey");
90     config.put("userauth.gssapi-with-mic", "com.jcraft.jsch.UserAuthGSSAPIWithMIC");
91     config.put("gssapi-with-mic.krb5", "com.jcraft.jsch.jgss.GSSContextKrb5");
92
93     config.put("zlib", "com.jcraft.jsch.jcraft.Compression");
94     config.put("zlib@openssh.com", "com.jcraft.jsch.jcraft.Compression");
95
96     config.put("StrictHostKeyChecking", "ask");
97     config.put("HashKnownHosts", "no");
98     //config.put("HashKnownHosts", "yes");
99
config.put("PreferredAuthentications", "gssapi-with-mic,publickey,keyboard-interactive,password");
100   }
101   java.util.Vector JavaDoc pool=new java.util.Vector JavaDoc();
102   java.util.Vector JavaDoc identities=new java.util.Vector JavaDoc();
103   private HostKeyRepository known_hosts=null;
104
105   private static final Logger DEVNULL=new Logger(){
106       public boolean isEnabled(int level){return false;}
107       public void log(int level, String JavaDoc message){}
108     };
109   static Logger logger=DEVNULL;
110
111   public JSch(){
112
113     try{
114       String JavaDoc osname=(String JavaDoc)(System.getProperties().get("os.name"));
115       if(osname!=null && osname.equals("Mac OS X")){
116         config.put("hmac-sha1", "com.jcraft.jsch.jcraft.HMACSHA1");
117         config.put("hmac-md5", "com.jcraft.jsch.jcraft.HMACMD5");
118         config.put("hmac-md5-96", "com.jcraft.jsch.jcraft.HMACMD596");
119         config.put("hmac-sha1-96", "com.jcraft.jsch.jcraft.HMACSHA196");
120       }
121     }
122     catch(Exception JavaDoc e){
123     }
124
125   }
126
127   public Session getSession(String JavaDoc username, String JavaDoc host) throws JSchException { return getSession(username, host, 22); }
128   public Session getSession(String JavaDoc username, String JavaDoc host, int port) throws JSchException {
129     if(username==null){
130       throw new JSchException("username must not be null.");
131     }
132     if(host==null){
133       throw new JSchException("host must not be null.");
134     }
135     Session s=new Session(this);
136     s.setUserName(username);
137     s.setHost(host);
138     s.setPort(port);
139     //pool.addElement(s);
140
return s;
141   }
142
143   protected void addSession(Session session){
144     synchronized(pool){
145       pool.addElement(session);
146     }
147   }
148
149   protected boolean removeSession(Session session){
150     synchronized(pool){
151       return pool.remove(session);
152     }
153   }
154   public void setHostKeyRepository(HostKeyRepository hkrepo){
155     known_hosts=hkrepo;
156   }
157
158   public void setKnownHosts(String JavaDoc filename) throws JSchException{
159     if(known_hosts==null) known_hosts=new KnownHosts(this);
160     if(known_hosts instanceof KnownHosts){
161       synchronized(known_hosts){
162     ((KnownHosts)known_hosts).setKnownHosts(filename);
163       }
164     }
165   }
166
167   public void setKnownHosts(InputStream JavaDoc stream) throws JSchException{
168     if(known_hosts==null) known_hosts=new KnownHosts(this);
169     if(known_hosts instanceof KnownHosts){
170       synchronized(known_hosts){
171     ((KnownHosts)known_hosts).setKnownHosts(stream);
172       }
173     }
174   }
175
176   public HostKeyRepository getHostKeyRepository(){
177     if(known_hosts==null) known_hosts=new KnownHosts(this);
178     return known_hosts;
179   }
180
181   public void addIdentity(String JavaDoc prvkey) throws JSchException{
182     addIdentity(prvkey, (byte[])null);
183   }
184
185   public void addIdentity(String JavaDoc prvkey, String JavaDoc passphrase) throws JSchException{
186     byte[] _passphrase=null;
187     if(passphrase!=null){
188       _passphrase=Util.str2byte(passphrase);
189     }
190     addIdentity(prvkey, _passphrase);
191     if(_passphrase!=null)
192       Util.bzero(_passphrase);
193   }
194
195   public void addIdentity(String JavaDoc prvkey, byte[] passphrase) throws JSchException{
196     Identity identity=IdentityFile.newInstance(prvkey, null, this);
197     addIdentity(identity, passphrase);
198   }
199   public void addIdentity(String JavaDoc prvkey, String JavaDoc pubkey, byte[] passphrase) throws JSchException{
200     Identity identity=IdentityFile.newInstance(prvkey, pubkey, this);
201     addIdentity(identity, passphrase);
202   }
203
204   public void addIdentity(String JavaDoc name, byte[]prvkey, byte[]pubkey, byte[] passphrase) throws JSchException{
205     Identity identity=IdentityFile.newInstance(name, prvkey, pubkey, this);
206     addIdentity(identity, passphrase);
207   }
208
209   public void addIdentity(Identity identity, byte[] passphrase) throws JSchException{
210     if(passphrase!=null){
211       try{
212         byte[] goo=new byte[passphrase.length];
213         System.arraycopy(passphrase, 0, goo, 0, passphrase.length);
214         passphrase=goo;
215         identity.setPassphrase(passphrase);
216       }
217       finally{
218         Util.bzero(passphrase);
219       }
220     }
221     synchronized(identities){
222       if(!identities.contains(identity)){
223     identities.addElement(identity);
224       }
225     }
226   }
227
228   public void removeIdentity(String JavaDoc name) throws JSchException{
229     synchronized(identities){
230       for(int i=0; i<identities.size(); i++){
231         Identity identity=(Identity)(identities.elementAt(i));
232     if(!identity.getName().equals(name))
233           continue;
234         identities.removeElement(identity);
235         identity.clear();
236         break;
237       }
238     }
239   }
240
241   public Vector JavaDoc getIdentityNames() throws JSchException{
242     Vector JavaDoc foo=new Vector JavaDoc();
243     synchronized(identities){
244       for(int i=0; i<identities.size(); i++){
245         Identity identity=(Identity)(identities.elementAt(i));
246         foo.addElement(identity.getName());
247       }
248     }
249     return foo;
250   }
251
252   public void removeAllIdentity() throws JSchException{
253     synchronized(identities){
254       Vector JavaDoc foo=getIdentityNames();
255       for(int i=0; i<foo.size(); i++){
256         String JavaDoc name=((String JavaDoc)foo.elementAt(i));
257         removeIdentity(name);
258       }
259     }
260   }
261
262   String JavaDoc getConfig(String JavaDoc key){
263     return (String JavaDoc)(config.get(key));
264   }
265
266   private java.util.Vector JavaDoc proxies;
267   void setProxy(String JavaDoc hosts, Proxy proxy){
268     java.lang.String JavaDoc[] patterns=Util.split(hosts, ",");
269     if(proxies==null){proxies=new java.util.Vector JavaDoc();}
270     synchronized(proxies){
271       for(int i=0; i<patterns.length; i++){
272     if(proxy==null){
273       proxies.insertElementAt(null, 0);
274       proxies.insertElementAt(patterns[i].getBytes(), 0);
275     }
276     else{
277       proxies.addElement(patterns[i].getBytes());
278       proxies.addElement(proxy);
279     }
280       }
281     }
282   }
283   Proxy getProxy(String JavaDoc host){
284     if(proxies==null)return null;
285     byte[] _host=host.getBytes();
286     synchronized(proxies){
287       for(int i=0; i<proxies.size(); i+=2){
288     if(Util.glob(((byte[])proxies.elementAt(i)), _host)){
289       return (Proxy)(proxies.elementAt(i+1));
290     }
291       }
292     }
293     return null;
294   }
295   void removeProxy(){
296     proxies=null;
297   }
298
299   public static void setConfig(java.util.Hashtable JavaDoc newconf){
300     synchronized(config){
301       for(java.util.Enumeration JavaDoc e=newconf.keys() ; e.hasMoreElements() ;) {
302     String JavaDoc key=(String JavaDoc)(e.nextElement());
303     config.put(key, (String JavaDoc)(newconf.get(key)));
304       }
305     }
306   }
307
308   public static void setLogger(Logger logger){
309     if(logger==null) JSch.logger=DEVNULL;
310     JSch.logger=logger;
311   }
312   static Logger getLogger(){
313     return logger;
314   }
315 }
316
Popular Tags