UserAuthPassword.java

Go to the documentation of this file.
00001 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
00002 /*
00003 Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
00004 
00005 Redistribution and use in source and binary forms, with or without
00006 modification, are permitted provided that the following conditions are met:
00007 
00008   1. Redistributions of source code must retain the above copyright notice,
00009      this list of conditions and the following disclaimer.
00010 
00011   2. Redistributions in binary form must reproduce the above copyright 
00012      notice, this list of conditions and the following disclaimer in 
00013      the documentation and/or other materials provided with the distribution.
00014 
00015   3. The names of the authors may not be used to endorse or promote products
00016      derived from this software without specific prior written permission.
00017 
00018 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
00019 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
00020 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
00021 INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
00022 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00023 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00024 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00025 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00026 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
00027 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028 */
00029 
00030 package com.jcraft.jsch;
00031 
00032 class UserAuthPassword extends UserAuth{
00033   private final int SSH_MSG_USERAUTH_PASSWD_CHANGEREQ=60;
00034 
00035   public boolean start(Session session) throws Exception{
00036     super.start(session);
00037 
00038     byte[] password=session.password;
00039     String dest=username+"@"+session.host;
00040     if(session.port!=22){
00041       dest+=(":"+session.port);
00042     }
00043 
00044     try{
00045 
00046     while(true){
00047       if(password==null){
00048     if(userinfo==null){
00049       //throw new JSchException("USERAUTH fail");
00050       return false;
00051     }
00052     if(!userinfo.promptPassword("Password for "+dest)){
00053       throw new JSchAuthCancelException("password");
00054       //break;
00055     }
00056 
00057     String _password=userinfo.getPassword();
00058     if(_password==null){
00059       throw new JSchAuthCancelException("password");
00060       //break;
00061     }
00062         password=Util.str2byte(_password);
00063       }
00064 
00065       byte[] _username=null;
00066       _username=Util.str2byte(username);
00067 
00068       // send
00069       // byte      SSH_MSG_USERAUTH_REQUEST(50)
00070       // string    user name
00071       // string    service name ("ssh-connection")
00072       // string    "password"
00073       // boolen    FALSE
00074       // string    plaintext password (ISO-10646 UTF-8)
00075       packet.reset();
00076       buf.putByte((byte)SSH_MSG_USERAUTH_REQUEST);
00077       buf.putString(_username);
00078       buf.putString(Util.str2byte("ssh-connection"));
00079       buf.putString(Util.str2byte("password"));
00080       buf.putByte((byte)0);
00081       buf.putString(password);
00082       session.write(packet);
00083 
00084       loop:
00085       while(true){
00086     buf=session.read(buf);
00087         int command=buf.getCommand()&0xff;
00088 
00089     if(command==SSH_MSG_USERAUTH_SUCCESS){
00090       return true;
00091     }
00092     if(command==SSH_MSG_USERAUTH_BANNER){
00093       buf.getInt(); buf.getByte(); buf.getByte();
00094       byte[] _message=buf.getString();
00095       byte[] lang=buf.getString();
00096           String message=Util.byte2str(_message);
00097       if(userinfo!=null){
00098         userinfo.showMessage(message);
00099       }
00100       continue loop;
00101     }
00102     if(command==SSH_MSG_USERAUTH_PASSWD_CHANGEREQ){
00103       buf.getInt(); buf.getByte(); buf.getByte(); 
00104       byte[] instruction=buf.getString();
00105       byte[] tag=buf.getString();
00106       if(userinfo==null || 
00107              !(userinfo instanceof UIKeyboardInteractive)){
00108             if(userinfo!=null){
00109               userinfo.showMessage("Password must be changed.");
00110             }
00111             return false;
00112           }
00113 
00114           UIKeyboardInteractive kbi=(UIKeyboardInteractive)userinfo;
00115           String[] response;
00116           String name="Password Change Required";
00117           String[] prompt={"New Password: "};
00118           boolean[] echo={false};
00119           response=kbi.promptKeyboardInteractive(dest,
00120                                                  name,
00121                                                  Util.byte2str(instruction),
00122                                                  prompt,
00123                                                  echo);
00124           if(response==null){
00125             throw new JSchAuthCancelException("password");
00126           }
00127 
00128           byte[] newpassword=Util.str2byte(response[0]);
00129 
00130           // send
00131           // byte      SSH_MSG_USERAUTH_REQUEST(50)
00132           // string    user name
00133           // string    service name ("ssh-connection")
00134           // string    "password"
00135           // boolen    TRUE
00136           // string    plaintext old password (ISO-10646 UTF-8)
00137           // string    plaintext new password (ISO-10646 UTF-8)
00138           packet.reset();
00139           buf.putByte((byte)SSH_MSG_USERAUTH_REQUEST);
00140           buf.putString(_username);
00141           buf.putString(Util.str2byte("ssh-connection"));
00142           buf.putString(Util.str2byte("password"));
00143           buf.putByte((byte)1);
00144           buf.putString(password);
00145           buf.putString(newpassword);
00146           Util.bzero(newpassword);
00147           response=null;
00148           session.write(packet);
00149       continue loop;
00150         }
00151     if(command==SSH_MSG_USERAUTH_FAILURE){
00152       buf.getInt(); buf.getByte(); buf.getByte(); 
00153       byte[] foo=buf.getString();
00154       int partial_success=buf.getByte();
00155       //System.err.println(new String(foo)+
00156       //         " partial_success:"+(partial_success!=0));
00157       if(partial_success!=0){
00158         throw new JSchPartialAuthException(Util.byte2str(foo));
00159       }
00160       break;
00161     }
00162     else{
00163           //System.err.println("USERAUTH fail ("+buf.getCommand()+")");
00164 //    throw new JSchException("USERAUTH fail ("+buf.getCommand()+")");
00165       return false;
00166     }
00167       }
00168 
00169       if(password!=null){
00170         Util.bzero(password);
00171         password=null;
00172       }
00173 
00174     }
00175 
00176     }
00177     finally{
00178       if(password!=null){
00179         Util.bzero(password);
00180         password=null;
00181       }
00182     }
00183 
00184     //throw new JSchException("USERAUTH fail");
00185     //return false;
00186   }
00187 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1