1 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
3 Copyright (c) 2002-2010 ymnk, JCraft,Inc. All rights reserved.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
8 1. Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
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.
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.
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.
30 package com.jcraft.jsch;
32 import java.io.InputStream;
33 import java.util.Vector;
36 static java.util.Hashtable config=new java.util.Hashtable();
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");
43 config.put("cipher.s2c",
44 "aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc,aes192-cbc,aes256-cbc");
45 config.put("cipher.c2s",
46 "aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc,aes192-cbc,aes256-cbc");
48 config.put("mac.s2c", "hmac-md5,hmac-sha1,hmac-sha1-96,hmac-md5-96");
49 config.put("mac.c2s", "hmac-md5,hmac-sha1,hmac-sha1-96,hmac-md5-96");
50 config.put("compression.s2c", "none");
51 // config.put("compression.s2c", "zlib@openssh.com,zlib,none");
52 config.put("compression.c2s", "none");
53 // config.put("compression.c2s", "zlib@openssh.com,zlib,none");
55 config.put("lang.s2c", "");
56 config.put("lang.c2s", "");
58 config.put("compression_level", "6");
60 config.put("diffie-hellman-group-exchange-sha1",
61 "com.jcraft.jsch.DHGEX");
62 config.put("diffie-hellman-group1-sha1",
63 "com.jcraft.jsch.DHG1");
65 config.put("dh", "com.jcraft.jsch.jce.DH");
66 config.put("3des-cbc", "com.jcraft.jsch.jce.TripleDESCBC");
67 config.put("blowfish-cbc", "com.jcraft.jsch.jce.BlowfishCBC");
68 config.put("hmac-sha1", "com.jcraft.jsch.jce.HMACSHA1");
69 config.put("hmac-sha1-96", "com.jcraft.jsch.jce.HMACSHA196");
70 config.put("hmac-md5", "com.jcraft.jsch.jce.HMACMD5");
71 config.put("hmac-md5-96", "com.jcraft.jsch.jce.HMACMD596");
72 config.put("sha-1", "com.jcraft.jsch.jce.SHA1");
73 config.put("md5", "com.jcraft.jsch.jce.MD5");
74 config.put("signature.dss", "com.jcraft.jsch.jce.SignatureDSA");
75 config.put("signature.rsa", "com.jcraft.jsch.jce.SignatureRSA");
76 config.put("keypairgen.dsa", "com.jcraft.jsch.jce.KeyPairGenDSA");
77 config.put("keypairgen.rsa", "com.jcraft.jsch.jce.KeyPairGenRSA");
78 config.put("random", "com.jcraft.jsch.jce.Random");
80 config.put("none", "com.jcraft.jsch.CipherNone");
82 config.put("aes128-cbc", "com.jcraft.jsch.jce.AES128CBC");
83 config.put("aes192-cbc", "com.jcraft.jsch.jce.AES192CBC");
84 config.put("aes256-cbc", "com.jcraft.jsch.jce.AES256CBC");
86 config.put("aes128-ctr", "com.jcraft.jsch.jce.AES128CTR");
87 config.put("aes192-ctr", "com.jcraft.jsch.jce.AES192CTR");
88 config.put("aes256-ctr", "com.jcraft.jsch.jce.AES256CTR");
89 config.put("3des-ctr", "com.jcraft.jsch.jce.TripleDESCTR");
90 config.put("arcfour", "com.jcraft.jsch.jce.ARCFOUR");
91 config.put("arcfour128", "com.jcraft.jsch.jce.ARCFOUR128");
92 config.put("arcfour256", "com.jcraft.jsch.jce.ARCFOUR256");
94 config.put("userauth.none", "com.jcraft.jsch.UserAuthNone");
95 config.put("userauth.password", "com.jcraft.jsch.UserAuthPassword");
96 config.put("userauth.keyboard-interactive", "com.jcraft.jsch.UserAuthKeyboardInteractive");
97 config.put("userauth.publickey", "com.jcraft.jsch.UserAuthPublicKey");
99 config.put("zlib", "com.jcraft.jsch.jcraft.Compression");
100 config.put("zlib@openssh.com", "com.jcraft.jsch.jcraft.Compression");
102 config.put("StrictHostKeyChecking", "ask");
103 config.put("HashKnownHosts", "no");
104 //config.put("HashKnownHosts", "yes");
105 config.put("PreferredAuthentications", "gssapi-with-mic,publickey,keyboard-interactive,password");
107 config.put("CheckCiphers", "aes256-ctr,aes192-ctr,aes128-ctr,aes256-cbc,aes192-cbc,aes128-cbc,3des-ctr,arcfour,arcfour128,arcfour256");
109 java.util.Vector pool=new java.util.Vector();
110 java.util.Vector identities=new java.util.Vector();
111 private HostKeyRepository known_hosts=null;
113 private static final Logger DEVNULL=new Logger(){
114 public boolean isEnabled(int level){return false;}
115 public void log(int level, String message){}
117 static Logger logger=DEVNULL;
122 String osname=(String)(System.getProperties().get("os.name"));
123 if(osname!=null && osname.equals("Mac OS X")){
124 config.put("hmac-sha1", "com.jcraft.jsch.jcraft.HMACSHA1");
125 config.put("hmac-md5", "com.jcraft.jsch.jcraft.HMACMD5");
126 config.put("hmac-md5-96", "com.jcraft.jsch.jcraft.HMACMD596");
127 config.put("hmac-sha1-96", "com.jcraft.jsch.jcraft.HMACSHA196");
135 public Session getSession(String username, String host) throws JSchException { return getSession(username, host, 22); }
136 public Session getSession(String username, String host, int port) throws JSchException {
138 throw new JSchException("username must not be null.");
141 throw new JSchException("host must not be null.");
143 Session s=new Session(this);
144 s.setUserName(username);
147 //pool.addElement(s);
151 protected void addSession(Session session){
153 pool.addElement(session);
157 protected boolean removeSession(Session session){
159 return pool.remove(session);
162 public void setHostKeyRepository(HostKeyRepository hkrepo){
166 public void setKnownHosts(String filename) throws JSchException{
167 if(known_hosts==null) known_hosts=new KnownHosts(this);
168 if(known_hosts instanceof KnownHosts){
169 synchronized(known_hosts){
170 ((KnownHosts)known_hosts).setKnownHosts(filename);
175 public void setKnownHosts(InputStream stream) throws JSchException{
176 if(known_hosts==null) known_hosts=new KnownHosts(this);
177 if(known_hosts instanceof KnownHosts){
178 synchronized(known_hosts){
179 ((KnownHosts)known_hosts).setKnownHosts(stream);
184 public HostKeyRepository getHostKeyRepository(){
185 if(known_hosts==null) known_hosts=new KnownHosts(this);
189 public void addIdentity(String prvkey) throws JSchException{
190 addIdentity(prvkey, (byte[])null);
193 public void addIdentity(String prvkey, String passphrase) throws JSchException{
194 byte[] _passphrase=null;
195 if(passphrase!=null){
196 _passphrase=Util.str2byte(passphrase);
198 addIdentity(prvkey, _passphrase);
199 if(_passphrase!=null)
200 Util.bzero(_passphrase);
203 public void addIdentity(String prvkey, byte[] passphrase) throws JSchException{
204 Identity identity=IdentityFile.newInstance(prvkey, null, this);
205 addIdentity(identity, passphrase);
207 public void addIdentity(String prvkey, String pubkey, byte[] passphrase) throws JSchException{
208 Identity identity=IdentityFile.newInstance(prvkey, pubkey, this);
209 addIdentity(identity, passphrase);
212 public void addIdentity(String name, byte[]prvkey, byte[]pubkey, byte[] passphrase) throws JSchException{
213 Identity identity=IdentityFile.newInstance(name, prvkey, pubkey, this);
214 addIdentity(identity, passphrase);
217 public void addIdentity(Identity identity, byte[] passphrase) throws JSchException{
218 if(passphrase!=null){
220 byte[] goo=new byte[passphrase.length];
221 System.arraycopy(passphrase, 0, goo, 0, passphrase.length);
223 identity.setPassphrase(passphrase);
226 Util.bzero(passphrase);
229 synchronized(identities){
230 if(!identities.contains(identity)){
231 identities.addElement(identity);
236 public void removeIdentity(String name) throws JSchException{
237 synchronized(identities){
238 for(int i=0; i<identities.size(); i++){
239 Identity identity=(Identity)(identities.elementAt(i));
240 if(!identity.getName().equals(name))
242 identities.removeElement(identity);
249 public Vector getIdentityNames() throws JSchException{
250 Vector foo=new Vector();
251 synchronized(identities){
252 for(int i=0; i<identities.size(); i++){
253 Identity identity=(Identity)(identities.elementAt(i));
254 foo.addElement(identity.getName());
260 public void removeAllIdentity() throws JSchException{
261 synchronized(identities){
262 Vector foo=getIdentityNames();
263 for(int i=0; i<foo.size(); i++){
264 String name=((String)foo.elementAt(i));
265 removeIdentity(name);
270 public static String getConfig(String key){
271 synchronized(config){
272 return (String)(config.get(key));
276 public static void setConfig(java.util.Hashtable newconf){
277 synchronized(config){
278 for(java.util.Enumeration e=newconf.keys() ; e.hasMoreElements() ;) {
279 String key=(String)(e.nextElement());
280 config.put(key, (String)(newconf.get(key)));
285 public static void setConfig(String key, String value){
286 config.put(key, value);
289 public static void setLogger(Logger logger){
290 if(logger==null) JSch.logger=DEVNULL;
293 static Logger getLogger(){