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 UserAuthPassword : UserAuth{
00035 internal UserInfo userinfo;
00036 internal UserAuthPassword(UserInfo userinfo){
00037 this.userinfo=userinfo;
00038 }
00039
00040 public override bool start(Session session) {
00041
00042
00043 Packet packet=session.packet;
00044 Buffer buf=session.buf;
00045 String username=session.username;
00046 String password=session.password;
00047 String dest=username+"@"+session.host;
00048 if(session.port!=22){
00049 dest+=(":"+session.port);
00050 }
00051
00052 while(true){
00053 if(password==null){
00054 if(userinfo==null){
00055
00056 return false;
00057 }
00058 if(!userinfo.promptPassword("Password for "+dest)){
00059 throw new JSchAuthCancelException("password");
00060
00061 }
00062 password=userinfo.getPassword();
00063 if(password==null){
00064 throw new JSchAuthCancelException("password");
00065
00066 }
00067 }
00068
00069 byte[] _username=null;
00070 try{ _username=Util.getBytesUTF8(username); }
00071 catch{
00072 _username=Util.getBytes(username);
00073 }
00074
00075 byte[] _password=null;
00076 try{ _password=Util.getBytesUTF8(password); }
00077 catch{
00078 _password=Util.getBytes(password);
00079 }
00080
00081
00082
00083
00084
00085
00086
00087
00088 packet.reset();
00089 buf.putByte((byte)Session.SSH_MSG_USERAUTH_REQUEST);
00090 buf.putString(_username);
00091 buf.putString(Util.getBytes("ssh-connection"));
00092 buf.putString(Util.getBytes("password"));
00093 buf.putByte((byte)0);
00094 buf.putString(_password);
00095 session.write(packet);
00096
00097 loop:
00098 while(true){
00099
00100
00101
00102 buf=session.read(buf);
00103
00104 if(buf.buffer[5]==Session.SSH_MSG_USERAUTH_SUCCESS){
00105 return true;
00106 }
00107 if(buf.buffer[5]==Session.SSH_MSG_USERAUTH_BANNER){
00108 buf.getInt(); buf.getByte(); buf.getByte();
00109 byte[] _message=buf.getString();
00110 byte[] lang=buf.getString();
00111 String message=null;
00112 try{ message=Util.getStringUTF8(_message); }
00113 catch{
00114 message=Util.getString(_message);
00115 }
00116 if(userinfo!=null){
00117 userinfo.showMessage(message);
00118 }
00119 goto loop;
00120 }
00121 if(buf.buffer[5]==Session.SSH_MSG_USERAUTH_FAILURE){
00122 buf.getInt(); buf.getByte(); buf.getByte();
00123 byte[] foo=buf.getString();
00124 int partial_success=buf.getByte();
00125
00126
00127 if(partial_success!=0){
00128 throw new JSchPartialAuthException(Util.getString(foo));
00129 }
00130 break;
00131 }
00132 else{
00133
00134
00135 return false;
00136 }
00137 }
00138 password=null;
00139 }
00140
00141
00142 }
00143 }
00144
00145 }