1 2 29 30 package com.jcraft.jsch; 31 32 import java.io.InputStream ; 33 import java.util.Vector ; 34 35 public class JSch{ 36 static java.util.Hashtable config=new java.util.Hashtable (); 37 static{ 38 config.put("kex", "diffie-hellman-group1-sha1,diffie-hellman-group-exchange-sha1"); 40 config.put("server_host_key", "ssh-rsa,ssh-dss"); 41 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.c2s", "none"); 51 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 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("PreferredAuthentications", "gssapi-with-mic,publickey,keyboard-interactive,password"); 100 } 101 java.util.Vector pool=new java.util.Vector (); 102 java.util.Vector identities=new java.util.Vector (); 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 message){} 108 }; 109 static Logger logger=DEVNULL; 110 111 public JSch(){ 112 113 try{ 114 String osname=(String )(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 e){ 123 } 124 125 } 126 127 public Session getSession(String username, String host) throws JSchException { return getSession(username, host, 22); } 128 public Session getSession(String username, String 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 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 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 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 prvkey) throws JSchException{ 182 addIdentity(prvkey, (byte[])null); 183 } 184 185 public void addIdentity(String prvkey, String 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 prvkey, byte[] passphrase) throws JSchException{ 196 Identity identity=IdentityFile.newInstance(prvkey, null, this); 197 addIdentity(identity, passphrase); 198 } 199 public void addIdentity(String prvkey, String pubkey, byte[] passphrase) throws JSchException{ 200 Identity identity=IdentityFile.newInstance(prvkey, pubkey, this); 201 addIdentity(identity, passphrase); 202 } 203 204 public void addIdentity(String 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 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 getIdentityNames() throws JSchException{ 242 Vector foo=new Vector (); 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 foo=getIdentityNames(); 255 for(int i=0; i<foo.size(); i++){ 256 String name=((String )foo.elementAt(i)); 257 removeIdentity(name); 258 } 259 } 260 } 261 262 String getConfig(String key){ 263 return (String )(config.get(key)); 264 } 265 266 private java.util.Vector proxies; 267 void setProxy(String hosts, Proxy proxy){ 268 java.lang.String [] patterns=Util.split(hosts, ","); 269 if(proxies==null){proxies=new java.util.Vector ();} 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 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 newconf){ 300 synchronized(config){ 301 for(java.util.Enumeration e=newconf.keys() ; e.hasMoreElements() ;) { 302 String key=(String )(e.nextElement()); 303 config.put(key, (String )(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 |