00001 using System;
00002
00003 namespace Tamir.SharpSsh.jsch
00004 {
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 class UserAuthKeyboardInteractive : UserAuth{
00035 internal UserInfo userinfo;
00036 internal UserAuthKeyboardInteractive(UserInfo userinfo){
00037 this.userinfo=userinfo;
00038 }
00039
00040 public override bool start(Session session) {
00041
00042 Packet packet=session.packet;
00043 Buffer buf=session.buf;
00044 String username=session.username;
00045 String dest=username+"@"+session.host;
00046 if(session.port!=22){
00047 dest+=(":"+session.port);
00048 }
00049
00050 bool cancel=false;
00051
00052 byte[] _username=null;
00053 try{ _username=System.Text.Encoding.UTF8.GetBytes(username); }
00054 catch{
00055 _username=Util.getBytes(username);
00056 }
00057
00058 while(true){
00059
00060
00061
00062
00063
00064
00065
00066 packet.reset();
00067 buf.putByte((byte)Session.SSH_MSG_USERAUTH_REQUEST);
00068 buf.putString(_username);
00069 buf.putString(Util.getBytes("ssh-connection"));
00070
00071 buf.putString(Util.getBytes("keyboard-interactive"));
00072 buf.putString(Util.getBytes(""));
00073 buf.putString(Util.getBytes(""));
00074 session.write(packet);
00075
00076 bool firsttime=true;
00077 loop:
00078 while(true){
00079
00080
00081
00082 try{ buf=session.read(buf); }
00083 catch(JSchException e){
00084 e.GetType();
00085 return false;
00086 }
00087 catch(System.IO.IOException e){
00088 e.GetType();
00089 return false;
00090 }
00091
00092 if(buf.buffer[5]==Session.SSH_MSG_USERAUTH_SUCCESS){
00093 return true;
00094 }
00095 if(buf.buffer[5]==Session.SSH_MSG_USERAUTH_BANNER){
00096 buf.getInt(); buf.getByte(); buf.getByte();
00097 byte[] _message=buf.getString();
00098 byte[] lang=buf.getString();
00099 String message=null;
00100 try{ message=Util.getStringUTF8(_message); }
00101 catch{
00102 message=Util.getString(_message);
00103 }
00104 if(userinfo!=null){
00105 userinfo.showMessage(message);
00106 }
00107 goto loop;
00108 }
00109 if(buf.buffer[5]==Session.SSH_MSG_USERAUTH_FAILURE){
00110 buf.getInt(); buf.getByte(); buf.getByte();
00111 byte[] foo=buf.getString();
00112 int partial_success=buf.getByte();
00113
00114
00115
00116 if(partial_success!=0){
00117 throw new JSchPartialAuthException(Util.getString(foo));
00118 }
00119
00120 if(firsttime){
00121 throw new JSchException("USERAUTH KI is not supported");
00122
00123
00124 }
00125 break;
00126 }
00127 if(buf.buffer[5]==Session.SSH_MSG_USERAUTH_INFO_REQUEST){
00128 firsttime=false;
00129 buf.getInt(); buf.getByte(); buf.getByte();
00130 String name=Util.getString(buf.getString());
00131 String instruction=Util.getString(buf.getString());
00132 String languate_tag=Util.getString(buf.getString());
00133 int num=buf.getInt();
00134
00135
00136
00137
00138 String[] prompt=new String[num];
00139 bool[] echo=new bool[num];
00140 for(int i=0; i<num; i++){
00141 prompt[i]=Util.getString(buf.getString());
00142 echo[i]=(buf.getByte()!=0);
00143
00144 }
00145
00146 String[] response=null;
00147 if(num>0
00148 ||(name.Length>0 || instruction.Length>0)
00149 ){
00150 UIKeyboardInteractive kbi=(UIKeyboardInteractive)userinfo;
00151 if(userinfo!=null){
00152 response=kbi.promptKeyboardInteractive(dest,
00153 name,
00154 instruction,
00155 prompt,
00156 echo);
00157 }
00158 }
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168 packet.reset();
00169 buf.putByte((byte)Session.SSH_MSG_USERAUTH_INFO_RESPONSE);
00170 if(num>0 &&
00171 (response==null ||
00172 num!=response.Length)){
00173 buf.putInt(0);
00174 if(response==null)
00175 cancel=true;
00176 }
00177 else{
00178 buf.putInt(num);
00179 for(int i=0; i<num; i++){
00180
00181 buf.putString(Util.getBytes(response[i]));
00182 }
00183 }
00184 session.write(packet);
00185 if(cancel)
00186 break;
00187
00188 goto loop;
00189 }
00190
00191 return false;
00192 }
00193 if(cancel){
00194 throw new JSchAuthCancelException("keyboard-interactive");
00195
00196 }
00197 }
00198
00199 }
00200 }
00201
00202 }