1 2 29 30 package com.jcraft.jsch; 31 32 class UserAuthPassword extends UserAuth{ 33 private final int SSH_MSG_USERAUTH_PASSWD_CHANGEREQ=60; 34 35 public boolean start(Session session, UserInfo userinfo) throws Exception { 36 this.userinfo=userinfo; 37 Packet packet=session.packet; 40 Buffer buf=session.buf; 41 final String username=session.username; 42 byte[] password=session.password; 43 String dest=username+"@"+session.host; 44 if(session.port!=22){ 45 dest+=(":"+session.port); 46 } 47 48 try{ 49 50 while(true){ 51 if(password==null){ 52 if(userinfo==null){ 53 return false; 55 } 56 if(!userinfo.promptPassword("Password for "+dest)){ 57 throw new JSchAuthCancelException("password"); 58 } 60 61 String _password=userinfo.getPassword(); 62 if(_password==null){ 63 throw new JSchAuthCancelException("password"); 64 } 66 password=Util.str2byte(_password); 67 } 68 69 byte[] _username=null; 70 _username=Util.str2byte(username); 71 72 packet.reset(); 80 buf.putByte((byte)SSH_MSG_USERAUTH_REQUEST); 81 buf.putString(_username); 82 buf.putString("ssh-connection".getBytes()); 83 buf.putString("password".getBytes()); 84 buf.putByte((byte)0); 85 buf.putString(password); 86 session.write(packet); 87 88 loop: 89 while(true){ 90 buf=session.read(buf); 94 if(buf.buffer[5]==SSH_MSG_USERAUTH_SUCCESS){ 96 return true; 97 } 98 if(buf.buffer[5]==SSH_MSG_USERAUTH_BANNER){ 99 buf.getInt(); buf.getByte(); buf.getByte(); 100 byte[] _message=buf.getString(); 101 byte[] lang=buf.getString(); 102 String message=Util.byte2str(_message); 103 if(userinfo!=null){ 104 userinfo.showMessage(message); 105 } 106 continue loop; 107 } 108 if(buf.buffer[5]==SSH_MSG_USERAUTH_PASSWD_CHANGEREQ){ 109 buf.getInt(); buf.getByte(); buf.getByte(); 110 byte[] instruction=buf.getString(); 111 byte[] tag=buf.getString(); 112 if(userinfo==null || 113 !(userinfo instanceof UIKeyboardInteractive)){ 114 if(userinfo!=null){ 115 userinfo.showMessage("Password must be changed."); 116 } 117 return false; 118 } 119 120 UIKeyboardInteractive kbi=(UIKeyboardInteractive)userinfo; 121 String [] response; 122 String name="Password Change Required"; 123 String [] prompt={"New Password: "}; 124 boolean[] echo={false}; 125 response=kbi.promptKeyboardInteractive(dest, 126 name, 127 new String (instruction), 128 prompt, 129 echo); 130 if(response==null){ 131 throw new JSchAuthCancelException("password"); 132 } 133 134 byte[] newpassword=response[0].getBytes(); 135 136 packet.reset(); 145 buf.putByte((byte)SSH_MSG_USERAUTH_REQUEST); 146 buf.putString(_username); 147 buf.putString("ssh-connection".getBytes()); 148 buf.putString("password".getBytes()); 149 buf.putByte((byte)1); 150 buf.putString(password); 151 buf.putString(newpassword); 152 Util.bzero(newpassword); 153 response=null; 154 session.write(packet); 155 continue loop; 156 } 157 if(buf.buffer[5]==SSH_MSG_USERAUTH_FAILURE){ 158 buf.getInt(); buf.getByte(); buf.getByte(); 159 byte[] foo=buf.getString(); 160 int partial_success=buf.getByte(); 161 if(partial_success!=0){ 164 throw new JSchPartialAuthException(new String (foo)); 165 } 166 break; 167 } 168 else{ 169 return false; 172 } 173 } 174 175 if(password!=null){ 176 Util.bzero(password); 177 password=null; 178 } 179 180 } 181 182 } 183 finally{ 184 if(password!=null){ 185 Util.bzero(password); 186 password=null; 187 } 188 } 189 190 } 193 } 194 | Popular Tags |