53
53
* @author <a href="http://mina.apache.org">Apache MINA Project</a>
54
54
*/
55
55
public class PASS extends AbstractCommand {
56
-
57
56
private final Logger LOG = LoggerFactory .getLogger (PASS .class );
58
57
59
58
/**
@@ -64,46 +63,41 @@ public class PASS extends AbstractCommand {
64
63
public void execute (final FtpIoSession session ,
65
64
final FtpServerContext context , final FtpRequest request )
66
65
throws IOException , FtpException {
67
-
68
66
boolean success = false ;
67
+ ServerFtpStatistics stat = (ServerFtpStatistics ) context .getFtpStatistics ();
69
68
70
- ServerFtpStatistics stat = (ServerFtpStatistics ) context
71
- .getFtpStatistics ();
72
69
try {
73
-
74
70
// reset state variables
75
71
session .resetState ();
76
72
77
73
// argument check
78
74
String password = request .getArgument ();
79
75
80
-
81
76
// check user name
82
77
String userName = session .getUserArgument ();
83
78
84
- if (userName == null && session .getUser () == null ) {
79
+ if (( userName == null ) && ( session .getUser () == null ) ) {
85
80
session .write (LocalizedFtpReply .translate (session , request , context ,
86
- FtpReply .REPLY_503_BAD_SEQUENCE_OF_COMMANDS , "PASS" ,
87
- null ));
81
+ FtpReply .REPLY_503_BAD_SEQUENCE_OF_COMMANDS , "PASS" , null ));
82
+
88
83
return ;
89
84
}
90
85
91
86
// already logged-in
92
87
if (session .isLoggedIn ()) {
93
88
session .write (LocalizedFtpReply .translate (session , request , context ,
94
- FtpReply .REPLY_202_COMMAND_NOT_IMPLEMENTED , "PASS" ,
95
- null ));
89
+ FtpReply .REPLY_202_COMMAND_NOT_IMPLEMENTED , "PASS" , null ));
90
+
96
91
return ;
97
92
}
98
93
99
94
// anonymous login limit check
95
+ boolean anonymous = UserManager .ANONYMOUS .equals (userName );
100
96
101
- boolean anonymous = userName != null
102
- && userName .equals ("anonymous" );
103
97
if (anonymous ) {
104
98
int currAnonLogin = stat .getCurrentAnonymousLoginNumber ();
105
- int maxAnonLogin = context .getConnectionConfig ()
106
- . getMaxAnonymousLogins ();
99
+ int maxAnonLogin = context .getConnectionConfig (). getMaxAnonymousLogins ();
100
+
107
101
if (maxAnonLogin == 0 ) {
108
102
LOG .debug ("Currently {} anonymous users logged in, unlimited allowed" , currAnonLogin );
109
103
} else {
@@ -112,58 +106,53 @@ public void execute(final FtpIoSession session,
112
106
113
107
if (currAnonLogin >= maxAnonLogin ) {
114
108
LOG .debug ("Too many anonymous users logged in, user will be disconnected" );
115
- session
116
- .write (LocalizedFtpReply
117
- .translate (
118
- session ,
119
- request ,
120
- context ,
109
+ session .write (LocalizedFtpReply .translate (session , request , context ,
121
110
FtpReply .REPLY_421_SERVICE_NOT_AVAILABLE_CLOSING_CONTROL_CONNECTION ,
122
111
"PASS.anonymous" , null ));
112
+
123
113
return ;
124
114
}
125
115
}
126
116
127
117
// login limit check
128
118
int currLogin = stat .getCurrentLoginNumber ();
129
119
int maxLogin = context .getConnectionConfig ().getMaxLogins ();
120
+
130
121
if (maxLogin == 0 ) {
131
122
LOG .debug ("Currently {} users logged in, unlimited allowed" , currLogin );
132
123
} else {
133
124
LOG .debug ("Currently {} out of {} users logged in" , currLogin , maxLogin );
134
125
}
126
+
135
127
if (maxLogin != 0 && currLogin >= maxLogin ) {
136
128
LOG .debug ("Too many users logged in, user will be disconnected" );
137
- session
138
- .write (LocalizedFtpReply
139
- .translate (
140
- session ,
141
- request ,
142
- context ,
129
+ session .write (LocalizedFtpReply .translate (
130
+ session , request , context ,
143
131
FtpReply .REPLY_421_SERVICE_NOT_AVAILABLE_CLOSING_CONTROL_CONNECTION ,
144
132
"PASS.login" , null ));
133
+
145
134
return ;
146
135
}
147
136
148
137
// authenticate user
149
138
UserManager userManager = context .getUserManager ();
150
139
User authenticatedUser = null ;
140
+
151
141
try {
152
142
UserMetadata userMetadata = new UserMetadata ();
153
143
154
144
if (session .getRemoteAddress () instanceof InetSocketAddress ) {
155
- userMetadata .setInetAddress (((InetSocketAddress ) session
156
- .getRemoteAddress ()).getAddress ());
145
+ userMetadata .setInetAddress (((InetSocketAddress ) session .getRemoteAddress ()).getAddress ());
157
146
}
158
- userMetadata . setCertificateChain ( session
159
- .getClientCertificates ());
147
+
148
+ userMetadata . setCertificateChain ( session .getClientCertificates ());
160
149
161
150
Authentication auth ;
151
+
162
152
if (anonymous ) {
163
153
auth = new AnonymousAuthentication (userMetadata );
164
154
} else {
165
- auth = new UsernamePasswordAuthentication (userName ,
166
- password , userMetadata );
155
+ auth = new UsernamePasswordAuthentication (userName , password , userMetadata );
167
156
}
168
157
169
158
authenticatedUser = userManager .authenticate (auth );
@@ -182,14 +171,9 @@ public void execute(final FtpIoSession session,
182
171
183
172
if (authenticatedUser != null ) {
184
173
if (!authenticatedUser .getEnabled ()) {
185
- session
186
- .write (LocalizedFtpReply
187
- .translate (
188
- session ,
189
- request ,
190
- context ,
191
- FtpReply .REPLY_530_NOT_LOGGED_IN ,
192
- "PASS" , null ));
174
+ session .write (LocalizedFtpReply .translate (
175
+ session , request , context , FtpReply .REPLY_530_NOT_LOGGED_IN , "PASS" , null ));
176
+
193
177
return ;
194
178
}
195
179
@@ -207,8 +191,7 @@ public void execute(final FtpIoSession session,
207
191
session .setUserArgument (oldUserArgument );
208
192
session .setMaxIdleTime (oldMaxIdleTime );
209
193
210
- delayAfterLoginFailure (context .getConnectionConfig ()
211
- .getLoginFailureDelay ());
194
+ delayAfterLoginFailure (context .getConnectionConfig ().getLoginFailureDelay ());
212
195
213
196
LOG .warn ("Login failure - " + userName );
214
197
session .write (LocalizedFtpReply .translate (session , request , context ,
@@ -218,10 +201,9 @@ public void execute(final FtpIoSession session,
218
201
session .increaseFailedLogins ();
219
202
220
203
// kick the user if the max number of failed logins is reached
221
- int maxAllowedLoginFailues = context .getConnectionConfig ()
222
- .getMaxLoginFailures ();
223
- if (maxAllowedLoginFailues != 0
224
- && session .getFailedLogins () >= maxAllowedLoginFailues ) {
204
+ int maxAllowedLoginFailues = context .getConnectionConfig ().getMaxLoginFailures ();
205
+
206
+ if ((maxAllowedLoginFailues != 0 ) && (session .getFailedLogins () >= maxAllowedLoginFailues )) {
225
207
LOG .warn ("User exceeded the number of allowed failed logins, session will be closed" );
226
208
227
209
session .close (false ).awaitUninterruptibly (10000 );
@@ -232,22 +214,20 @@ public void execute(final FtpIoSession session,
232
214
233
215
// update different objects
234
216
FileSystemFactory fmanager = context .getFileSystemManager ();
235
- FileSystemView fsview = fmanager
236
- .createFileSystemView (authenticatedUser );
217
+ FileSystemView fsview = fmanager .createFileSystemView (authenticatedUser );
237
218
session .setLogin (fsview );
238
219
stat .setLogin (session );
239
220
240
221
// everything is fine - send login ok message
241
222
session .write (LocalizedFtpReply .translate (session , request , context ,
242
- FtpReply .REPLY_230_USER_LOGGED_IN , "PASS" , userName ));
223
+ FtpReply .REPLY_230_USER_LOGGED_IN , "PASS" , userName ));
224
+
243
225
if (anonymous ) {
244
- LOG .info ("Anonymous login success - " + password );
226
+ LOG .info ("Anonymous login success" );
245
227
} else {
246
- LOG .info ("Login success - " + userName );
228
+ LOG .info ("Login success - {}" , userName );
247
229
}
248
-
249
230
} finally {
250
-
251
231
// if login failed - reset user
252
232
if (!success ) {
253
233
session .reinitialize ();
@@ -256,7 +236,6 @@ public void execute(final FtpIoSession session,
256
236
}
257
237
258
238
private void delayAfterLoginFailure (final int loginFailureDelay ) {
259
-
260
239
if (loginFailureDelay > 0 ) {
261
240
LOG .debug ("Waiting for {} milliseconds due to login failure" , loginFailureDelay );
262
241
0 commit comments