KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcraft > jsch > UserAuthPassword


1 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
2 /*
3 Copyright (c) 2002,2003,2004,2005,2006 ymnk, JCraft,Inc. All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8   1. Redistributions of source code must retain the above copyright notice,
9      this list of conditions and the following disclaimer.
10
11   2. Redistributions in binary form must reproduce the above copyright
12      notice, this list of conditions and the following disclaimer in
13      the documentation and/or other materials provided with the distribution.
14
15   3. The names of the authors may not be used to endorse or promote products
16      derived from this software without specific prior written permission.
17
18 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
19 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
21 INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
22 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */

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 JavaDoc{
36     this.userinfo=userinfo;
37 // super.start(session);
38
//System.err.println("UserAuthPassword: start");
39
Packet packet=session.packet;
40     Buffer buf=session.buf;
41     final String JavaDoc username=session.username;
42     byte[] password=session.password;
43     String JavaDoc 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       //throw new JSchException("USERAUTH fail");
54
return false;
55     }
56     if(!userinfo.promptPassword("Password for "+dest)){
57       throw new JSchAuthCancelException("password");
58       //break;
59
}
60
61     String JavaDoc _password=userinfo.getPassword();
62     if(_password==null){
63       throw new JSchAuthCancelException("password");
64       //break;
65
}
66         password=Util.str2byte(_password);
67       }
68
69       byte[] _username=null;
70       _username=Util.str2byte(username);
71
72       // send
73
// byte SSH_MSG_USERAUTH_REQUEST(50)
74
// string user name
75
// string service name ("ssh-connection")
76
// string "password"
77
// boolen FALSE
78
// string plaintext password (ISO-10646 UTF-8)
79
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     // receive
91
// byte SSH_MSG_USERAUTH_SUCCESS(52)
92
// string service name
93
buf=session.read(buf);
94         //System.err.println("read: 52 ? "+ buf.buffer[5]);
95
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 JavaDoc 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 JavaDoc[] response;
122           String JavaDoc name="Password Change Required";
123           String JavaDoc[] prompt={"New Password: "};
124           boolean[] echo={false};
125           response=kbi.promptKeyboardInteractive(dest,
126                                                  name,
127                                                  new String JavaDoc(instruction),
128                                                  prompt,
129                                                  echo);
130           if(response==null){
131             throw new JSchAuthCancelException("password");
132           }
133
134           byte[] newpassword=response[0].getBytes();
135
136           // send
137
// byte SSH_MSG_USERAUTH_REQUEST(50)
138
// string user name
139
// string service name ("ssh-connection")
140
// string "password"
141
// boolen TRUE
142
// string plaintext old password (ISO-10646 UTF-8)
143
// string plaintext new password (ISO-10646 UTF-8)
144
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       //System.err.println(new String(foo)+
162
// " partial_success:"+(partial_success!=0));
163
if(partial_success!=0){
164         throw new JSchPartialAuthException(new String JavaDoc(foo));
165       }
166       break;
167     }
168     else{
169           //System.err.println("USERAUTH fail ("+buf.buffer[5]+")");
170
// throw new JSchException("USERAUTH fail ("+buf.buffer[5]+")");
171
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     //throw new JSchException("USERAUTH fail");
191
//return false;
192
}
193 }
194
Popular Tags