1 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
3 Copyright (c) 2002-2010 ymnk, JCraft,Inc. All rights reserved.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
8 1. Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
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.
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.
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.
30 package com.jcraft.jsch;
32 class UserAuthKeyboardInteractive extends UserAuth{
33 public boolean start(Session session) throws Exception{
36 if(userinfo!=null && !(userinfo instanceof UIKeyboardInteractive)){
40 String dest=username+"@"+session.host;
42 dest+=(":"+session.port);
44 byte[] password=session.password;
48 byte[] _username=null;
49 _username=Util.str2byte(username);
53 // byte SSH_MSG_USERAUTH_REQUEST(50)
54 // string user name (ISO-10646 UTF-8, as defined in [RFC-2279])
55 // string service name (US-ASCII) "ssh-userauth" ? "ssh-connection"
56 // string "keyboard-interactive" (US-ASCII)
57 // string language tag (as defined in [RFC-3066])
58 // string submethods (ISO-10646 UTF-8)
60 buf.putByte((byte)SSH_MSG_USERAUTH_REQUEST);
61 buf.putString(_username);
62 buf.putString(Util.str2byte("ssh-connection"));
63 //buf.putString("ssh-userauth".getBytes());
64 buf.putString(Util.str2byte("keyboard-interactive"));
65 buf.putString(Util.empty);
66 buf.putString(Util.empty);
67 session.write(packet);
69 boolean firsttime=true;
72 buf=session.read(buf);
73 int command=buf.getCommand()&0xff;
75 if(command==SSH_MSG_USERAUTH_SUCCESS){
78 if(command==SSH_MSG_USERAUTH_BANNER){
79 buf.getInt(); buf.getByte(); buf.getByte();
80 byte[] _message=buf.getString();
81 byte[] lang=buf.getString();
82 String message=Util.byte2str(_message);
84 userinfo.showMessage(message);
88 if(command==SSH_MSG_USERAUTH_FAILURE){
89 buf.getInt(); buf.getByte(); buf.getByte();
90 byte[] foo=buf.getString();
91 int partial_success=buf.getByte();
92 // System.err.println(new String(foo)+
93 // " partial_success:"+(partial_success!=0));
95 if(partial_success!=0){
96 throw new JSchPartialAuthException(Util.byte2str(foo));
101 //throw new JSchException("USERAUTH KI is not supported");
106 if(command==SSH_MSG_USERAUTH_INFO_REQUEST){
108 buf.getInt(); buf.getByte(); buf.getByte();
109 String name=Util.byte2str(buf.getString());
110 String instruction=Util.byte2str(buf.getString());
111 String languate_tag=Util.byte2str(buf.getString());
112 int num=buf.getInt();
113 String[] prompt=new String[num];
114 boolean[] echo=new boolean[num];
115 for(int i=0; i<num; i++){
116 prompt[i]=Util.byte2str(buf.getString());
117 echo[i]=(buf.getByte()!=0);
120 byte[][] response=null;
125 prompt[0].toLowerCase().startsWith("password:")){
126 response=new byte[1][];
127 response[0]=password;
131 ||(name.length()>0 || instruction.length()>0)
134 UIKeyboardInteractive kbi=(UIKeyboardInteractive)userinfo;
135 String[] _response=kbi.promptKeyboardInteractive(dest,
141 response=new byte[_response.length][];
142 for(int i=0; i<_response.length; i++){
143 response[i]=Util.str2byte(_response[i]);
149 // byte SSH_MSG_USERAUTH_INFO_RESPONSE(61)
151 // string response[1] (ISO-10646 UTF-8)
153 // string response[num-responses] (ISO-10646 UTF-8)
155 buf.putByte((byte)SSH_MSG_USERAUTH_INFO_RESPONSE);
157 (response==null || // cancel
158 num!=response.length)){
161 // working around the bug in OpenSSH ;-<
163 for(int i=0; i<num; i++){
164 buf.putString(Util.empty);
176 for(int i=0; i<num; i++){
177 buf.putString(response[i]);
180 session.write(packet);
187 //throw new JSchException("USERAUTH fail ("+command+")");
191 throw new JSchAuthCancelException("keyboard-interactive");