comparison app/src/main/java/ch/ethz/ssh2/auth/ServerAuthenticationManager.java @ 438:d29cce60f393

migrate from Eclipse to Android Studio
author Carl Byington <carl@five-ten-sg.com>
date Thu, 03 Dec 2015 11:23:55 -0800
parents src/ch/ethz/ssh2/auth/ServerAuthenticationManager.java@071eccdff8ea
children
comparison
equal deleted inserted replaced
437:208b31032318 438:d29cce60f393
1
2 package ch.ethz.ssh2.auth;
3
4 import java.io.IOException;
5 import java.util.Arrays;
6 import java.util.HashSet;
7 import java.util.Set;
8
9 import ch.ethz.ssh2.AuthenticationResult;
10 import ch.ethz.ssh2.PacketTypeException;
11 import ch.ethz.ssh2.ServerAuthenticationCallback;
12 import ch.ethz.ssh2.channel.ChannelManager;
13 import ch.ethz.ssh2.packets.PacketServiceAccept;
14 import ch.ethz.ssh2.packets.PacketServiceRequest;
15 import ch.ethz.ssh2.packets.PacketUserauthBanner;
16 import ch.ethz.ssh2.packets.PacketUserauthFailure;
17 import ch.ethz.ssh2.packets.PacketUserauthSuccess;
18 import ch.ethz.ssh2.packets.Packets;
19 import ch.ethz.ssh2.packets.TypesReader;
20 import ch.ethz.ssh2.server.ServerConnectionState;
21 import ch.ethz.ssh2.transport.MessageHandler;
22
23 public class ServerAuthenticationManager implements MessageHandler {
24 private final ServerConnectionState state;
25
26 public ServerAuthenticationManager(ServerConnectionState state) {
27 this.state = state;
28 state.tm.registerMessageHandler(this, 0, 255);
29 }
30
31 private void sendresult(AuthenticationResult result) throws IOException {
32 if (AuthenticationResult.SUCCESS == result) {
33 PacketUserauthSuccess pus = new PacketUserauthSuccess();
34 state.tm.sendAsynchronousMessage(pus.getPayload());
35 state.tm.removeMessageHandler(this);
36 state.tm.registerMessageHandler(this, 50, 79);
37 state.cm = new ChannelManager(state);
38 state.flag_auth_completed = true;
39 }
40 else {
41 Set<String> remaining_methods = new HashSet<String>();
42
43 if (state.cb_auth != null) {
44 remaining_methods.addAll(Arrays.asList(
45 state.cb_auth.getRemainingAuthMethods(state.conn)));
46 }
47
48 PacketUserauthFailure puf = new PacketUserauthFailure(remaining_methods,
49 AuthenticationResult.PARTIAL_SUCCESS == result);
50 state.tm.sendAsynchronousMessage(puf.getPayload());
51 }
52 }
53
54 public void handleFailure(final IOException failure) {
55 //
56 }
57
58 public void handleMessage(byte[] msg) throws IOException {
59 /* Ignore all authentication messages after successful auth */
60 if (state.flag_auth_completed) {
61 return;
62 }
63
64 if (!state.flag_auth_serviceRequested) {
65 /* Must be PacketServiceRequest */
66 PacketServiceRequest psr = new PacketServiceRequest(msg);
67
68 if (!"ssh-userauth".equals(psr.getServiceName())) {
69 throw new IOException("SSH protocol error, expected ssh-userauth service request");
70 }
71
72 PacketServiceAccept psa = new PacketServiceAccept("ssh-userauth");
73 state.tm.sendAsynchronousMessage(psa.getPayload());
74 String banner = state.cb_auth.initAuthentication(state.conn);
75
76 if (banner != null) {
77 PacketUserauthBanner pub = new PacketUserauthBanner(banner);
78 state.tm.sendAsynchronousMessage(pub.getPayload());
79 }
80
81 state.flag_auth_serviceRequested = true;
82 return;
83 }
84
85 ServerAuthenticationCallback cb = state.cb_auth;
86 TypesReader tr = new TypesReader(msg);
87 int packet_type = tr.readByte();
88
89 if (packet_type == Packets.SSH_MSG_USERAUTH_REQUEST) {
90 String username = tr.readString("UTF-8");
91 String service = tr.readString();
92 String method = tr.readString();
93
94 if (!"ssh-connection".equals(service)) {
95 sendresult(AuthenticationResult.FAILURE);
96 return;
97 }
98
99 if ("none".equals(method)) {
100 if (cb != null) {
101 sendresult(cb.authenticateWithNone(state.conn, username));
102 return;
103 }
104 }
105
106 if ("password".equals(method)) {
107 boolean flag_change_pass = tr.readBoolean();
108
109 if (flag_change_pass) {
110 sendresult(AuthenticationResult.FAILURE);
111 return;
112 }
113
114 String password = tr.readString("UTF-8");
115
116 if (cb != null) {
117 sendresult(cb.authenticateWithPassword(state.conn, username, password));
118 return;
119 }
120 }
121
122 sendresult(AuthenticationResult.FAILURE);
123 return;
124 }
125
126 throw new PacketTypeException(packet_type);
127 }
128 }