]>
Commit | Line | Data |
---|---|---|
1 | /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */ | |
2 | /* | |
3 | Copyright (c) 2002-2010 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; | |
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-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", | |
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"); | |
47 | ||
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"); | |
54 | ||
55 | config.put("lang.s2c", ""); | |
56 | config.put("lang.c2s", ""); | |
57 | ||
58 | config.put("compression_level", "6"); | |
59 | ||
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"); | |
64 | ||
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"); | |
79 | ||
80 | config.put("none", "com.jcraft.jsch.CipherNone"); | |
81 | ||
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"); | |
85 | ||
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"); | |
93 | ||
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"); | |
98 | ||
99 | config.put("zlib", "com.jcraft.jsch.jcraft.Compression"); | |
100 | config.put("zlib@openssh.com", "com.jcraft.jsch.jcraft.Compression"); | |
101 | ||
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"); | |
106 | ||
107 | config.put("CheckCiphers", "aes256-ctr,aes192-ctr,aes128-ctr,aes256-cbc,aes192-cbc,aes128-cbc,3des-ctr,arcfour,arcfour128,arcfour256"); | |
108 | } | |
109 | java.util.Vector pool=new java.util.Vector(); | |
110 | java.util.Vector identities=new java.util.Vector(); | |
111 | private HostKeyRepository known_hosts=null; | |
112 | ||
113 | private static final Logger DEVNULL=new Logger(){ | |
114 | public boolean isEnabled(int level){return false;} | |
115 | public void log(int level, String message){} | |
116 | }; | |
117 | static Logger logger=DEVNULL; | |
118 | ||
119 | public JSch(){ | |
120 | ||
121 | try{ | |
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"); | |
128 | } | |
129 | } | |
130 | catch(Exception e){ | |
131 | } | |
132 | ||
133 | } | |
134 | ||
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 { | |
137 | if(username==null){ | |
138 | throw new JSchException("username must not be null."); | |
139 | } | |
140 | if(host==null){ | |
141 | throw new JSchException("host must not be null."); | |
142 | } | |
143 | Session s=new Session(this); | |
144 | s.setUserName(username); | |
145 | s.setHost(host); | |
146 | s.setPort(port); | |
147 | //pool.addElement(s); | |
148 | return s; | |
149 | } | |
150 | ||
151 | protected void addSession(Session session){ | |
152 | synchronized(pool){ | |
153 | pool.addElement(session); | |
154 | } | |
155 | } | |
156 | ||
157 | protected boolean removeSession(Session session){ | |
158 | synchronized(pool){ | |
159 | return pool.remove(session); | |
160 | } | |
161 | } | |
162 | public void setHostKeyRepository(HostKeyRepository hkrepo){ | |
163 | known_hosts=hkrepo; | |
164 | } | |
165 | ||
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); | |
171 | } | |
172 | } | |
173 | } | |
174 | ||
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); | |
180 | } | |
181 | } | |
182 | } | |
183 | ||
184 | public HostKeyRepository getHostKeyRepository(){ | |
185 | if(known_hosts==null) known_hosts=new KnownHosts(this); | |
186 | return known_hosts; | |
187 | } | |
188 | ||
189 | public void addIdentity(String prvkey) throws JSchException{ | |
190 | addIdentity(prvkey, (byte[])null); | |
191 | } | |
192 | ||
193 | public void addIdentity(String prvkey, String passphrase) throws JSchException{ | |
194 | byte[] _passphrase=null; | |
195 | if(passphrase!=null){ | |
196 | _passphrase=Util.str2byte(passphrase); | |
197 | } | |
198 | addIdentity(prvkey, _passphrase); | |
199 | if(_passphrase!=null) | |
200 | Util.bzero(_passphrase); | |
201 | } | |
202 | ||
203 | public void addIdentity(String prvkey, byte[] passphrase) throws JSchException{ | |
204 | Identity identity=IdentityFile.newInstance(prvkey, null, this); | |
205 | addIdentity(identity, passphrase); | |
206 | } | |
207 | public void addIdentity(String prvkey, String pubkey, byte[] passphrase) throws JSchException{ | |
208 | Identity identity=IdentityFile.newInstance(prvkey, pubkey, this); | |
209 | addIdentity(identity, passphrase); | |
210 | } | |
211 | ||
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); | |
215 | } | |
216 | ||
217 | public void addIdentity(Identity identity, byte[] passphrase) throws JSchException{ | |
218 | if(passphrase!=null){ | |
219 | try{ | |
220 | byte[] goo=new byte[passphrase.length]; | |
221 | System.arraycopy(passphrase, 0, goo, 0, passphrase.length); | |
222 | passphrase=goo; | |
223 | identity.setPassphrase(passphrase); | |
224 | } | |
225 | finally{ | |
226 | Util.bzero(passphrase); | |
227 | } | |
228 | } | |
229 | synchronized(identities){ | |
230 | if(!identities.contains(identity)){ | |
231 | identities.addElement(identity); | |
232 | } | |
233 | } | |
234 | } | |
235 | ||
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)) | |
241 | continue; | |
242 | identities.removeElement(identity); | |
243 | identity.clear(); | |
244 | break; | |
245 | } | |
246 | } | |
247 | } | |
248 | ||
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()); | |
255 | } | |
256 | } | |
257 | return foo; | |
258 | } | |
259 | ||
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); | |
266 | } | |
267 | } | |
268 | } | |
269 | ||
270 | public static String getConfig(String key){ | |
271 | synchronized(config){ | |
272 | return (String)(config.get(key)); | |
273 | } | |
274 | } | |
275 | ||
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))); | |
281 | } | |
282 | } | |
283 | } | |
284 | ||
285 | public static void setConfig(String key, String value){ | |
286 | config.put(key, value); | |
287 | } | |
288 | ||
289 | public static void setLogger(Logger logger){ | |
290 | if(logger==null) JSch.logger=DEVNULL; | |
291 | JSch.logger=logger; | |
292 | } | |
293 | static Logger getLogger(){ | |
294 | return logger; | |
295 | } | |
296 | } |