1 2 29 30 package com.jcraft.jsch; 31 32 class UserAuthKeyboardInteractive extends UserAuth{ 33 public boolean start(Session session, UserInfo userinfo) throws Exception { 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 username=session.username; 42 String 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 packet.reset(); 62 buf.putByte((byte)SSH_MSG_USERAUTH_REQUEST); 63 buf.putString(_username); 64 buf.putString("ssh-connection".getBytes()); 65 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 82 buf=session.read(buf); 83 84 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 message=null; 94 try{ message=new String (_message, "UTF-8"); } 95 catch(java.io.UnsupportedEncodingException e){ 96 message=new String (_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 110 if(partial_success!=0){ 111 throw new JSchPartialAuthException(new String (foo)); 112 } 113 114 if(firsttime){ 115 return false; 116 } 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 name=new String (buf.getString()); 125 String instruction=new String (buf.getString()); 126 String languate_tag=new String (buf.getString()); 127 int num=buf.getInt(); 128 String [] prompt=new String [num]; 133 boolean[] echo=new boolean[num]; 134 for(int i=0; i<num; i++){ 135 prompt[i]=new String (buf.getString()); 136 echo[i]=(buf.getByte()!=0); 137 } 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 [] _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 packet.reset(); 178 buf.putByte((byte)SSH_MSG_USERAUTH_INFO_RESPONSE); 179 if(num>0 && 180 (response==null || num!=response.length)){ 182 183 if(response==null){ 184 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 buf.putString(response[i]); 202 } 203 } 204 session.write(packet); 205 209 continue loop; 210 } 211 return false; 213 } 214 if(cancel){ 215 throw new JSchAuthCancelException("keyboard-interactive"); 216 } 218 } 219 } 221 } 222 | Popular Tags |