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: class UserAuthNone extends UserAuth {
033: private static final int SSH_MSG_SERVICE_ACCEPT = 6;
034: private String methods = null;
035:
036: public boolean start(Session session) throws Exception {
037: super .start(session);
038:
039: // send
040: // byte SSH_MSG_SERVICE_REQUEST(5)
041: // string service name "ssh-userauth"
042: packet.reset();
043: buf.putByte((byte) Session.SSH_MSG_SERVICE_REQUEST);
044: buf.putString("ssh-userauth".getBytes());
045: session.write(packet);
046:
047: if (JSch.getLogger().isEnabled(Logger.INFO)) {
048: JSch.getLogger().log(Logger.INFO,
049: "SSH_MSG_SERVICE_REQUEST sent");
050: }
051:
052: // receive
053: // byte SSH_MSG_SERVICE_ACCEPT(6)
054: // string service name
055: buf = session.read(buf);
056: int command = buf.getCommand();
057:
058: boolean result = (command == SSH_MSG_SERVICE_ACCEPT);
059:
060: if (JSch.getLogger().isEnabled(Logger.INFO)) {
061: JSch.getLogger().log(Logger.INFO,
062: "SSH_MSG_SERVICE_ACCEPT received");
063: }
064: if (!result)
065: return false;
066:
067: byte[] _username = null;
068: _username = Util.str2byte(username);
069:
070: // send
071: // byte SSH_MSG_USERAUTH_REQUEST(50)
072: // string user name
073: // string service name ("ssh-connection")
074: // string "none"
075: packet.reset();
076: buf.putByte((byte) SSH_MSG_USERAUTH_REQUEST);
077: buf.putString(_username);
078: buf.putString("ssh-connection".getBytes());
079: buf.putString("none".getBytes());
080: session.write(packet);
081:
082: loop: while (true) {
083: buf = session.read(buf);
084: command = buf.getCommand() & 0xff;
085:
086: if (command == SSH_MSG_USERAUTH_SUCCESS) {
087: return true;
088: }
089: if (command == SSH_MSG_USERAUTH_BANNER) {
090: buf.getInt();
091: buf.getByte();
092: buf.getByte();
093: byte[] _message = buf.getString();
094: byte[] lang = buf.getString();
095: String message = null;
096: try {
097: message = new String(_message, "UTF-8");
098: } catch (java.io.UnsupportedEncodingException e) {
099: message = new String(_message);
100: }
101: if (userinfo != null) {
102: try {
103: userinfo.showMessage(message);
104: } catch (RuntimeException ee) {
105: }
106: }
107: continue loop;
108: }
109: if (command == SSH_MSG_USERAUTH_FAILURE) {
110: buf.getInt();
111: buf.getByte();
112: buf.getByte();
113: byte[] foo = buf.getString();
114: int partial_success = buf.getByte();
115: methods = new String(foo);
116: //System.err.println("UserAuthNONE: "+methods+
117: // " partial_success:"+(partial_success!=0));
118: // if(partial_success!=0){
119: // throw new JSchPartialAuthException(new String(foo));
120: // }
121:
122: break;
123: } else {
124: // System.err.println("USERAUTH fail ("+command+")");
125: throw new JSchException("USERAUTH fail (" + command
126: + ")");
127: }
128: }
129: //throw new JSchException("USERAUTH fail");
130: return false;
131: }
132:
133: String getMethods() {
134: return methods;
135: }
136: }
|