JSch.java

Go to the documentation of this file.
00001 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
00002 /*
00003 Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
00004 
00005 Redistribution and use in source and binary forms, with or without
00006 modification, are permitted provided that the following conditions are met:
00007 
00008   1. Redistributions of source code must retain the above copyright notice,
00009      this list of conditions and the following disclaimer.
00010 
00011   2. Redistributions in binary form must reproduce the above copyright 
00012      notice, this list of conditions and the following disclaimer in 
00013      the documentation and/or other materials provided with the distribution.
00014 
00015   3. The names of the authors may not be used to endorse or promote products
00016      derived from this software without specific prior written permission.
00017 
00018 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
00019 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
00020 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
00021 INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
00022 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00023 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00024 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00025 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00026 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
00027 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028 */
00029 
00030 package com.jcraft.jsch;
00031 
00032 import java.io.InputStream;
00033 import java.util.Vector;
00034 
00049 public class JSch{
00050   static java.util.Hashtable config=new java.util.Hashtable();
00051   static{
00052 //  config.put("kex", "diffie-hellman-group-exchange-sha1");
00053     config.put("kex", "diffie-hellman-group1-sha1,diffie-hellman-group14-sha1,diffie-hellman-group-exchange-sha1");
00054     config.put("server_host_key", "ssh-rsa,ssh-dss");
00055 //    config.put("server_host_key", "ssh-dss,ssh-rsa");
00056 
00057     config.put("cipher.s2c", 
00058                "aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc,aes192-cbc,aes256-cbc");
00059     config.put("cipher.c2s",
00060                "aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc,aes192-cbc,aes256-cbc");
00061 
00062     config.put("mac.s2c", "hmac-md5,hmac-sha1,hmac-sha1-96,hmac-md5-96");
00063     config.put("mac.c2s", "hmac-md5,hmac-sha1,hmac-sha1-96,hmac-md5-96");
00064     config.put("compression.s2c", "none");
00065     // config.put("compression.s2c", "zlib@openssh.com,zlib,none");
00066     config.put("compression.c2s", "none");
00067     // config.put("compression.c2s", "zlib@openssh.com,zlib,none");
00068 
00069     config.put("lang.s2c", "");
00070     config.put("lang.c2s", "");
00071 
00072     config.put("compression_level", "6");
00073 
00074     config.put("diffie-hellman-group-exchange-sha1", 
00075                                 "com.jcraft.jsch.DHGEX");
00076     config.put("diffie-hellman-group1-sha1", 
00077                             "com.jcraft.jsch.DHG1");
00078     config.put("diffie-hellman-group14-sha1", 
00079                             "com.jcraft.jsch.DHG14");
00080 
00081     config.put("dh",            "com.jcraft.jsch.jce.DH");
00082     config.put("3des-cbc",      "com.jcraft.jsch.jce.TripleDESCBC");
00083     config.put("blowfish-cbc",  "com.jcraft.jsch.jce.BlowfishCBC");
00084     config.put("hmac-sha1",     "com.jcraft.jsch.jce.HMACSHA1");
00085     config.put("hmac-sha1-96",  "com.jcraft.jsch.jce.HMACSHA196");
00086     config.put("hmac-md5",      "com.jcraft.jsch.jce.HMACMD5");
00087     config.put("hmac-md5-96",   "com.jcraft.jsch.jce.HMACMD596");
00088     config.put("sha-1",         "com.jcraft.jsch.jce.SHA1");
00089     config.put("md5",           "com.jcraft.jsch.jce.MD5");
00090     config.put("signature.dss", "com.jcraft.jsch.jce.SignatureDSA");
00091     config.put("signature.rsa", "com.jcraft.jsch.jce.SignatureRSA");
00092     config.put("keypairgen.dsa",   "com.jcraft.jsch.jce.KeyPairGenDSA");
00093     config.put("keypairgen.rsa",   "com.jcraft.jsch.jce.KeyPairGenRSA");
00094     config.put("random",        "com.jcraft.jsch.jce.Random");
00095 
00096     config.put("none",           "com.jcraft.jsch.CipherNone");
00097 
00098     config.put("aes128-cbc",    "com.jcraft.jsch.jce.AES128CBC");
00099     config.put("aes192-cbc",    "com.jcraft.jsch.jce.AES192CBC");
00100     config.put("aes256-cbc",    "com.jcraft.jsch.jce.AES256CBC");
00101 
00102     config.put("aes128-ctr",    "com.jcraft.jsch.jce.AES128CTR");
00103     config.put("aes192-ctr",    "com.jcraft.jsch.jce.AES192CTR");
00104     config.put("aes256-ctr",    "com.jcraft.jsch.jce.AES256CTR");
00105     config.put("3des-ctr",      "com.jcraft.jsch.jce.TripleDESCTR");
00106     config.put("arcfour",      "com.jcraft.jsch.jce.ARCFOUR");
00107     config.put("arcfour128",      "com.jcraft.jsch.jce.ARCFOUR128");
00108     config.put("arcfour256",      "com.jcraft.jsch.jce.ARCFOUR256");
00109 
00110     config.put("userauth.none",    "com.jcraft.jsch.UserAuthNone");
00111     config.put("userauth.password",    "com.jcraft.jsch.UserAuthPassword");
00112     config.put("userauth.keyboard-interactive",    "com.jcraft.jsch.UserAuthKeyboardInteractive");
00113     config.put("userauth.publickey",    "com.jcraft.jsch.UserAuthPublicKey");
00114     config.put("userauth.gssapi-with-mic",    "com.jcraft.jsch.UserAuthGSSAPIWithMIC");
00115     config.put("gssapi-with-mic.krb5",    "com.jcraft.jsch.jgss.GSSContextKrb5");
00116 
00117     config.put("zlib",             "com.jcraft.jsch.jcraft.Compression");
00118     config.put("zlib@openssh.com", "com.jcraft.jsch.jcraft.Compression");
00119 
00120     config.put("StrictHostKeyChecking",  "ask");
00121     config.put("HashKnownHosts",  "no");
00122     //config.put("HashKnownHosts",  "yes");
00123     config.put("PreferredAuthentications", "gssapi-with-mic,publickey,keyboard-interactive,password");
00124 
00125     config.put("CheckCiphers", "aes256-ctr,aes192-ctr,aes128-ctr,aes256-cbc,aes192-cbc,aes128-cbc,3des-ctr,arcfour,arcfour128,arcfour256");
00126     config.put("CheckKexes", "diffie-hellman-group14-sha1");
00127   }
00128 
00133   java.util.Vector pool=new java.util.Vector();
00134   java.util.Vector identities=new java.util.Vector();
00135   private HostKeyRepository known_hosts=null;
00136 
00140   private static final Logger DEVNULL=new Logger(){
00141       public boolean isEnabled(int level){return false;}
00142       public void log(int level, String message){}
00143     };
00144   static Logger logger=DEVNULL;
00145 
00149   public JSch(){
00150 
00151     try{
00152       String osname=(String)(System.getProperties().get("os.name"));
00153       if(osname!=null && osname.equals("Mac OS X")){
00154         config.put("hmac-sha1",     "com.jcraft.jsch.jcraft.HMACSHA1"); 
00155         config.put("hmac-md5",      "com.jcraft.jsch.jcraft.HMACMD5"); 
00156         config.put("hmac-md5-96",   "com.jcraft.jsch.jcraft.HMACMD596"); 
00157         config.put("hmac-sha1-96",  "com.jcraft.jsch.jcraft.HMACSHA196"); 
00158       }
00159     }
00160     catch(Exception e){
00161     }
00162 
00163   }
00164 
00171   public Session getSession(String username, String host) throws JSchException { return getSession(username, host, 22); }
00172   
00180   public Session getSession(String username, String host, int port) throws JSchException {
00181     if(username==null){
00182       throw new JSchException("username must not be null.");
00183     }
00184     if(host==null){
00185       throw new JSchException("host must not be null.");
00186     }
00187     Session s=new Session(this); 
00188     s.setUserName(username);
00189     s.setHost(host);
00190     s.setPort(port);
00191     //pool.addElement(s);
00192     return s;
00193   }
00194 
00195 
00201   protected void addSession(Session session){
00202     synchronized(pool){
00203       pool.addElement(session);
00204     }
00205   }
00206 
00207   
00213   protected boolean removeSession(Session session){
00214     synchronized(pool){
00215       return pool.remove(session);
00216     }
00217   }
00218 
00224   public void setHostKeyRepository(HostKeyRepository hkrepo){
00225     known_hosts=hkrepo;
00226   }
00227 
00237   public void setKnownHosts(String filename) throws JSchException{
00238     // Fun fact: the implemenation of this method contains
00239     //           9 times the term "known hosts" (not counting
00240     //           this comment), and almost nothing else.
00241     //   (It's the same with the next method.)    -- P.E.
00242     if(known_hosts==null) known_hosts=new KnownHosts(this);
00243     if(known_hosts instanceof KnownHosts){
00244       synchronized(known_hosts){
00245     ((KnownHosts)known_hosts).setKnownHosts(filename); 
00246       }
00247     }
00248   }
00249 
00259   public void setKnownHosts(InputStream stream) throws JSchException{ 
00260     if(known_hosts==null) known_hosts=new KnownHosts(this);
00261     if(known_hosts instanceof KnownHosts){
00262       synchronized(known_hosts){
00263     ((KnownHosts)known_hosts).setKnownHosts(stream); 
00264       }
00265     }
00266   }
00267 
00275   public HostKeyRepository getHostKeyRepository(){ 
00276     if(known_hosts==null) known_hosts=new KnownHosts(this);
00277     return known_hosts; 
00278   }
00279 
00287   public void addIdentity(String prvkey) throws JSchException{
00288     addIdentity(prvkey, (byte[])null);
00289   }
00290 
00300   public void addIdentity(String prvkey, String passphrase) throws JSchException{
00301     byte[] _passphrase=null;
00302     if(passphrase!=null){
00303       _passphrase=Util.str2byte(passphrase);
00304     }
00305     addIdentity(prvkey, _passphrase);
00306     if(_passphrase!=null)
00307       Util.bzero(_passphrase);
00308   }
00309 
00318   public void addIdentity(String prvkey, byte[] passphrase) throws JSchException{
00319     Identity identity=IdentityFile.newInstance(prvkey, null, this);
00320     addIdentity(identity, passphrase);
00321   }
00322 
00330   public void addIdentity(String prvkey, String pubkey, byte[] passphrase) throws JSchException{
00331     Identity identity=IdentityFile.newInstance(prvkey, pubkey, this);
00332     addIdentity(identity, passphrase);
00333   }
00334 
00343   public void addIdentity(String name, byte[]prvkey, byte[]pubkey, byte[] passphrase) throws JSchException{
00344     Identity identity=IdentityFile.newInstance(name, prvkey, pubkey, this);
00345     addIdentity(identity, passphrase);
00346   }
00347 
00354   public void addIdentity(Identity identity, byte[] passphrase) throws JSchException{
00355     if(passphrase!=null){
00356       try{ 
00357         byte[] goo=new byte[passphrase.length];
00358         System.arraycopy(passphrase, 0, goo, 0, passphrase.length);
00359         passphrase=goo;
00360         identity.setPassphrase(passphrase); 
00361       }
00362       finally{
00363         Util.bzero(passphrase);
00364       }
00365     }
00366     synchronized(identities){
00367       if(!identities.contains(identity)){
00368     identities.addElement(identity);
00369       }
00370     }
00371   }
00372 
00383   public void removeIdentity(String name) throws JSchException{
00384     synchronized(identities){
00385       for(int i=0; i<identities.size(); i++){
00386         Identity identity=(Identity)(identities.elementAt(i));
00387     if(!identity.getName().equals(name))
00388           continue;
00389         // why not removeElementAt(i)?  -- P.E.
00390         identities.removeElement(identity);
00391         identity.clear();
00392         break;
00393       }
00394     }
00395   }
00396 
00397 
00403   public Vector getIdentityNames() throws JSchException{
00404     Vector foo=new Vector();
00405     synchronized(identities){
00406       for(int i=0; i<identities.size(); i++){
00407         Identity identity=(Identity)(identities.elementAt(i));
00408         foo.addElement(identity.getName());
00409       }
00410     }
00411     return foo;
00412   }
00413 
00414 
00419   public void removeAllIdentity() throws JSchException{
00420     synchronized(identities){
00421       Vector foo=getIdentityNames();
00422       for(int i=0; i<foo.size(); i++){
00423         String name=((String)foo.elementAt(i));
00424         removeIdentity(name);
00425       }
00426     }
00427   }
00428 
00437   public static String getConfig(String key){ 
00438     synchronized(config){
00439       return (String)(config.get(key));
00440     } 
00441   }
00442 
00451   public static void setConfig(java.util.Hashtable newconf){
00452     synchronized(config){
00453       for(java.util.Enumeration e=newconf.keys() ; e.hasMoreElements() ;) {
00454     String key=(String)(e.nextElement());
00455     config.put(key, (String)(newconf.get(key)));
00456       }
00457     }
00458   }
00459 
00692   public static void setConfig(String key, String value){
00693     config.put(key, value);
00694   }
00695 
00701   public static void setLogger(Logger logger){
00702     if(logger==null) logger=DEVNULL;
00703     JSch.logger=logger;
00704   }
00705 
00709   static Logger getLogger(){
00710     return logger;
00711   }
00712 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1