KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcraft > jsch > UserAuthKeyboardInteractive


1 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
2 /*
3 Copyright (c) 2002,2003,2004,2005,2006 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 class UserAuthKeyboardInteractive extends UserAuth{
33   public boolean start(Session session, UserInfo userinfo) throws Exception JavaDoc{
34     this.userinfo=userinfo;
35     if(!(userinfo instanceof UIKeyboardInteractive)){
36       return false;
37     }
38
39     Packet packet=session.packet;
40     Buffer buf=session.buf;
41     final String JavaDoc username=session.username;
42     String JavaDoc dest=username+"@"+session.host;
43     if(session.port!=22){
44       dest+=(":"+session.port);
45     }
46     byte[] password=session.password;
47
48     boolean cancel=false;
49
50     byte[] _username=null;
51     _username=Util.str2byte(username);
52
53     while(true){
54       // send
55
// byte SSH_MSG_USERAUTH_REQUEST(50)
56
// string user name (ISO-10646 UTF-8, as defined in [RFC-2279])
57
// string service name (US-ASCII) "ssh-userauth" ? "ssh-connection"
58
// string "keyboard-interactive" (US-ASCII)
59
// string language tag (as defined in [RFC-3066])
60
// string submethods (ISO-10646 UTF-8)
61
packet.reset();
62       buf.putByte((byte)SSH_MSG_USERAUTH_REQUEST);
63       buf.putString(_username);
64       buf.putString("ssh-connection".getBytes());
65       //buf.putString("ssh-userauth".getBytes());
66
buf.putString("keyboard-interactive".getBytes());
67       buf.putString("".getBytes());
68       buf.putString("".getBytes());
69       session.write(packet);
70
71       boolean firsttime=true;
72       loop:
73       while(true){
74 // try{ buf=session.read(buf); }
75
// catch(JSchException e){
76
// return false;
77
// }
78
// catch(java.io.IOException e){
79
// return false;
80
// }
81

82         buf=session.read(buf);
83
84         //System.err.println("read: 52 ? "+ buf.buffer[5]);
85

86     if(buf.buffer[5]==SSH_MSG_USERAUTH_SUCCESS){
87       return true;
88     }
89     if(buf.buffer[5]==SSH_MSG_USERAUTH_BANNER){
90       buf.getInt(); buf.getByte(); buf.getByte();
91       byte[] _message=buf.getString();
92       byte[] lang=buf.getString();
93       String JavaDoc message=null;
94       try{ message=new String JavaDoc(_message, "UTF-8"); }
95       catch(java.io.UnsupportedEncodingException JavaDoc e){
96         message=new String JavaDoc(_message);
97       }
98       if(userinfo!=null){
99         userinfo.showMessage(message);
100       }
101       continue loop;
102     }
103     if(buf.buffer[5]==SSH_MSG_USERAUTH_FAILURE){
104       buf.getInt(); buf.getByte(); buf.getByte();
105       byte[] foo=buf.getString();
106       int partial_success=buf.getByte();
107 // System.err.println(new String(foo)+
108
// " partial_success:"+(partial_success!=0));
109

110       if(partial_success!=0){
111         throw new JSchPartialAuthException(new String JavaDoc(foo));
112       }
113
114       if(firsttime){
115         return false;
116         //throw new JSchException("USERAUTH KI is not supported");
117
//cancel=true; // ??
118
}
119       break;
120     }
121     if(buf.buffer[5]==SSH_MSG_USERAUTH_INFO_REQUEST){
122       firsttime=false;
123       buf.getInt(); buf.getByte(); buf.getByte();
124       String JavaDoc name=new String JavaDoc(buf.getString());
125       String JavaDoc instruction=new String JavaDoc(buf.getString());
126       String JavaDoc languate_tag=new String JavaDoc(buf.getString());
127       int num=buf.getInt();
128 //System.err.println("name: "+name);
129
//System.err.println("instruction: "+instruction);
130
//System.err.println("lang: "+languate_tag);
131
//System.err.println("num: "+num);
132
String JavaDoc[] prompt=new String JavaDoc[num];
133       boolean[] echo=new boolean[num];
134       for(int i=0; i<num; i++){
135         prompt[i]=new String JavaDoc(buf.getString());
136         echo[i]=(buf.getByte()!=0);
137 //System.err.println(" "+prompt[i]+","+echo[i]);
138
}
139
140       byte[][] response=null;
141       if(num>0
142          ||(name.length()>0 || instruction.length()>0)
143          ){
144         UIKeyboardInteractive kbi=(UIKeyboardInteractive)userinfo;
145         if(userinfo!=null){
146               String JavaDoc[] _response=kbi.promptKeyboardInteractive(dest,
147                                                                name,
148                                                                instruction,
149                                                                prompt,
150                                                                echo);
151               if(_response!=null){
152                 response=new byte[_response.length][];
153                 for(int i=0; i<_response.length; i++){
154                   response[i]=Util.str2byte(_response[i]);
155                 }
156               }
157         }
158             else if(password!=null &&
159                     prompt.length==1 &&
160                     !echo[0] &&
161                     prompt[0].toLowerCase().startsWith("password:")){
162               response=new byte[1][];
163               response[0]=password;
164               password=null;
165             }
166       }
167
168       // byte SSH_MSG_USERAUTH_INFO_RESPONSE(61)
169
// int num-responses
170
// string response[1] (ISO-10646 UTF-8)
171
// ...
172
// string response[num-responses] (ISO-10646 UTF-8)
173
//if(response!=null)
174
//System.err.println("response.length="+response.length);
175
//else
176
//System.err.println("response is null");
177
packet.reset();
178       buf.putByte((byte)SSH_MSG_USERAUTH_INFO_RESPONSE);
179       if(num>0 &&
180          (response==null || // cancel
181
num!=response.length)){
182
183             if(response==null){
184               // working around the bug in OpenSSH ;-<
185
buf.putInt(num);
186               for(int i=0; i<num; i++){
187                 buf.putString("".getBytes());
188               }
189             }
190             else{
191               buf.putInt(0);
192             }
193
194         if(response==null)
195           cancel=true;
196       }
197       else{
198         buf.putInt(num);
199         for(int i=0; i<num; i++){
200 //System.err.println("response: |"+new String(response[i])+"| <- replace here with **** if you need");
201
buf.putString(response[i]);
202         }
203       }
204       session.write(packet);
205           /*
206       if(cancel)
207         break;
208           */

209       continue loop;
210     }
211     //throw new JSchException("USERAUTH fail ("+buf.buffer[5]+")");
212
return false;
213       }
214       if(cancel){
215     throw new JSchAuthCancelException("keyboard-interactive");
216     //break;
217
}
218     }
219     //return false;
220
}
221 }
222
Popular Tags