001: /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
002: /*
003: Copyright (c) 2002-2008 ymnk, JCraft,Inc. All rights reserved.
004:
005: Redistribution and use in source and binary forms, with or without
006: modification, are permitted provided that the following conditions are met:
007:
008: 1. Redistributions of source code must retain the above copyright notice,
009: this list of conditions and the following disclaimer.
010:
011: 2. Redistributions in binary form must reproduce the above copyright
012: notice, this list of conditions and the following disclaimer in
013: the documentation and/or other materials provided with the distribution.
014:
015: 3. The names of the authors may not be used to endorse or promote products
016: derived from this software without specific prior written permission.
017:
018: THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
019: INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
020: FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
021: INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
022: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
023: LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
024: OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
027: EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: */
029:
030: package com.jcraft.jsch;
031:
032: import java.io.InputStream;
033: import java.util.Vector;
034:
035: public class JSch {
036: static java.util.Hashtable config = new java.util.Hashtable();
037: static {
038: // config.put("kex", "diffie-hellman-group-exchange-sha1");
039: config
040: .put("kex",
041: "diffie-hellman-group1-sha1,diffie-hellman-group-exchange-sha1");
042: config.put("server_host_key", "ssh-rsa,ssh-dss");
043: // config.put("server_host_key", "ssh-dss,ssh-rsa");
044:
045: config
046: .put("cipher.s2c",
047: "aes128-cbc,3des-cbc,blowfish-cbc,aes192-cbc,aes256-cbc");
048: config
049: .put("cipher.c2s",
050: "aes128-cbc,3des-cbc,blowfish-cbc,aes192-cbc,aes256-cbc");
051:
052: config.put("mac.s2c",
053: "hmac-md5,hmac-sha1,hmac-sha1-96,hmac-md5-96");
054: config.put("mac.c2s",
055: "hmac-md5,hmac-sha1,hmac-sha1-96,hmac-md5-96");
056: config.put("compression.s2c", "none");
057: // config.put("compression.s2c", "zlib@openssh.com,zlib,none");
058: config.put("compression.c2s", "none");
059: // config.put("compression.c2s", "zlib@openssh.com,zlib,none");
060:
061: config.put("lang.s2c", "");
062: config.put("lang.c2s", "");
063:
064: config.put("compression_level", "6");
065:
066: config.put("diffie-hellman-group-exchange-sha1",
067: "com.jcraft.jsch.DHGEX");
068: config
069: .put("diffie-hellman-group1-sha1",
070: "com.jcraft.jsch.DHG1");
071:
072: config.put("dh", "com.jcraft.jsch.jce.DH");
073: config.put("3des-cbc", "com.jcraft.jsch.jce.TripleDESCBC");
074: config.put("blowfish-cbc", "com.jcraft.jsch.jce.BlowfishCBC");
075: config.put("hmac-sha1", "com.jcraft.jsch.jce.HMACSHA1");
076: config.put("hmac-sha1-96", "com.jcraft.jsch.jce.HMACSHA196");
077: config.put("hmac-md5", "com.jcraft.jsch.jce.HMACMD5");
078: config.put("hmac-md5-96", "com.jcraft.jsch.jce.HMACMD596");
079: config.put("sha-1", "com.jcraft.jsch.jce.SHA1");
080: config.put("md5", "com.jcraft.jsch.jce.MD5");
081: config.put("signature.dss", "com.jcraft.jsch.jce.SignatureDSA");
082: config.put("signature.rsa", "com.jcraft.jsch.jce.SignatureRSA");
083: config.put("keypairgen.dsa",
084: "com.jcraft.jsch.jce.KeyPairGenDSA");
085: config.put("keypairgen.rsa",
086: "com.jcraft.jsch.jce.KeyPairGenRSA");
087: config.put("random", "com.jcraft.jsch.jce.Random");
088:
089: config.put("none", "com.jcraft.jsch.CipherNone");
090:
091: config.put("aes128-cbc", "com.jcraft.jsch.jce.AES128CBC");
092: config.put("aes192-cbc", "com.jcraft.jsch.jce.AES192CBC");
093: config.put("aes256-cbc", "com.jcraft.jsch.jce.AES256CBC");
094: // config.put("cipher.s2c", "aes128-cbc,3des-cbc,blowfish-cbc");
095: // config.put("cipher.c2s", "aes128-cbc,3des-cbc,blowfish-cbc");
096:
097: config.put("userauth.none", "com.jcraft.jsch.UserAuthNone");
098: config.put("userauth.password",
099: "com.jcraft.jsch.UserAuthPassword");
100: config.put("userauth.keyboard-interactive",
101: "com.jcraft.jsch.UserAuthKeyboardInteractive");
102: config.put("userauth.publickey",
103: "com.jcraft.jsch.UserAuthPublicKey");
104: config.put("userauth.gssapi-with-mic",
105: "com.jcraft.jsch.UserAuthGSSAPIWithMIC");
106: config.put("gssapi-with-mic.krb5",
107: "com.jcraft.jsch.jgss.GSSContextKrb5");
108:
109: config.put("zlib", "com.jcraft.jsch.jcraft.Compression");
110: config.put("zlib@openssh.com",
111: "com.jcraft.jsch.jcraft.Compression");
112:
113: config.put("StrictHostKeyChecking", "ask");
114: config.put("HashKnownHosts", "no");
115: //config.put("HashKnownHosts", "yes");
116: config
117: .put("PreferredAuthentications",
118: "gssapi-with-mic,publickey,keyboard-interactive,password");
119:
120: config.put("CheckCiphers", "aes256-cbc,aes192-cbc,aes128-cbc");
121: }
122: java.util.Vector pool = new java.util.Vector();
123: java.util.Vector identities = new java.util.Vector();
124: private HostKeyRepository known_hosts = null;
125:
126: private static final Logger DEVNULL = new Logger() {
127: public boolean isEnabled(int level) {
128: return false;
129: }
130:
131: public void log(int level, String message) {
132: }
133: };
134: static Logger logger = DEVNULL;
135:
136: public JSch() {
137:
138: try {
139: String osname = (String) (System.getProperties()
140: .get("os.name"));
141: if (osname != null && osname.equals("Mac OS X")) {
142: config.put("hmac-sha1",
143: "com.jcraft.jsch.jcraft.HMACSHA1");
144: config
145: .put("hmac-md5",
146: "com.jcraft.jsch.jcraft.HMACMD5");
147: config.put("hmac-md5-96",
148: "com.jcraft.jsch.jcraft.HMACMD596");
149: config.put("hmac-sha1-96",
150: "com.jcraft.jsch.jcraft.HMACSHA196");
151: }
152: } catch (Exception e) {
153: }
154:
155: }
156:
157: public Session getSession(String username, String host)
158: throws JSchException {
159: return getSession(username, host, 22);
160: }
161:
162: public Session getSession(String username, String host, int port)
163: throws JSchException {
164: if (username == null) {
165: throw new JSchException("username must not be null.");
166: }
167: if (host == null) {
168: throw new JSchException("host must not be null.");
169: }
170: Session s = new Session(this );
171: s.setUserName(username);
172: s.setHost(host);
173: s.setPort(port);
174: //pool.addElement(s);
175: return s;
176: }
177:
178: protected void addSession(Session session) {
179: synchronized (pool) {
180: pool.addElement(session);
181: }
182: }
183:
184: protected boolean removeSession(Session session) {
185: synchronized (pool) {
186: return pool.remove(session);
187: }
188: }
189:
190: public void setHostKeyRepository(HostKeyRepository hkrepo) {
191: known_hosts = hkrepo;
192: }
193:
194: public void setKnownHosts(String filename) throws JSchException {
195: if (known_hosts == null)
196: known_hosts = new KnownHosts(this );
197: if (known_hosts instanceof KnownHosts) {
198: synchronized (known_hosts) {
199: ((KnownHosts) known_hosts).setKnownHosts(filename);
200: }
201: }
202: }
203:
204: public void setKnownHosts(InputStream stream) throws JSchException {
205: if (known_hosts == null)
206: known_hosts = new KnownHosts(this );
207: if (known_hosts instanceof KnownHosts) {
208: synchronized (known_hosts) {
209: ((KnownHosts) known_hosts).setKnownHosts(stream);
210: }
211: }
212: }
213:
214: public HostKeyRepository getHostKeyRepository() {
215: if (known_hosts == null)
216: known_hosts = new KnownHosts(this );
217: return known_hosts;
218: }
219:
220: public void addIdentity(String prvkey) throws JSchException {
221: addIdentity(prvkey, (byte[]) null);
222: }
223:
224: public void addIdentity(String prvkey, String passphrase)
225: throws JSchException {
226: byte[] _passphrase = null;
227: if (passphrase != null) {
228: _passphrase = Util.str2byte(passphrase);
229: }
230: addIdentity(prvkey, _passphrase);
231: if (_passphrase != null)
232: Util.bzero(_passphrase);
233: }
234:
235: public void addIdentity(String prvkey, byte[] passphrase)
236: throws JSchException {
237: Identity identity = IdentityFile
238: .newInstance(prvkey, null, this );
239: addIdentity(identity, passphrase);
240: }
241:
242: public void addIdentity(String prvkey, String pubkey,
243: byte[] passphrase) throws JSchException {
244: Identity identity = IdentityFile.newInstance(prvkey, pubkey,
245: this );
246: addIdentity(identity, passphrase);
247: }
248:
249: public void addIdentity(String name, byte[] prvkey, byte[] pubkey,
250: byte[] passphrase) throws JSchException {
251: Identity identity = IdentityFile.newInstance(name, prvkey,
252: pubkey, this );
253: addIdentity(identity, passphrase);
254: }
255:
256: public void addIdentity(Identity identity, byte[] passphrase)
257: throws JSchException {
258: if (passphrase != null) {
259: try {
260: byte[] goo = new byte[passphrase.length];
261: System.arraycopy(passphrase, 0, goo, 0,
262: passphrase.length);
263: passphrase = goo;
264: identity.setPassphrase(passphrase);
265: } finally {
266: Util.bzero(passphrase);
267: }
268: }
269: synchronized (identities) {
270: if (!identities.contains(identity)) {
271: identities.addElement(identity);
272: }
273: }
274: }
275:
276: public void removeIdentity(String name) throws JSchException {
277: synchronized (identities) {
278: for (int i = 0; i < identities.size(); i++) {
279: Identity identity = (Identity) (identities.elementAt(i));
280: if (!identity.getName().equals(name))
281: continue;
282: identities.removeElement(identity);
283: identity.clear();
284: break;
285: }
286: }
287: }
288:
289: public Vector getIdentityNames() throws JSchException {
290: Vector foo = new Vector();
291: synchronized (identities) {
292: for (int i = 0; i < identities.size(); i++) {
293: Identity identity = (Identity) (identities.elementAt(i));
294: foo.addElement(identity.getName());
295: }
296: }
297: return foo;
298: }
299:
300: public void removeAllIdentity() throws JSchException {
301: synchronized (identities) {
302: Vector foo = getIdentityNames();
303: for (int i = 0; i < foo.size(); i++) {
304: String name = ((String) foo.elementAt(i));
305: removeIdentity(name);
306: }
307: }
308: }
309:
310: String getConfig(String key) {
311: return (String) (config.get(key));
312: }
313:
314: public static void setConfig(java.util.Hashtable newconf) {
315: synchronized (config) {
316: for (java.util.Enumeration e = newconf.keys(); e
317: .hasMoreElements();) {
318: String key = (String) (e.nextElement());
319: config.put(key, (String) (newconf.get(key)));
320: }
321: }
322: }
323:
324: public static void setConfig(String key, String value) {
325: config.put(key, value);
326: }
327:
328: public static void setLogger(Logger logger) {
329: if (logger == null)
330: JSch.logger = DEVNULL;
331: JSch.logger = logger;
332: }
333:
334: static Logger getLogger() {
335: return logger;
336: }
337: }
|