Skip to content

Commit

Permalink
o Minor code refactoring
Browse files Browse the repository at this point in the history
o Fixed javadoc warnings
o Renamed some variables for clarity
o Slight code improvement
o Added a toString method to the Default%essageResource class
o Bumped up a couple of dependencies
  • Loading branch information
elecharny committed Jan 10, 2025
1 parent 583db0b commit 5ab035a
Show file tree
Hide file tree
Showing 13 changed files with 262 additions and 173 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@
* @author <a href="http://mina.apache.org">Apache MINA Project</a>
*/
public class CommandFactoryFactory {

private static final HashMap<String, Command> DEFAULT_COMMAND_MAP = new HashMap<>();

static {
Expand Down Expand Up @@ -116,6 +115,7 @@ public class CommandFactoryFactory {
DEFAULT_COMMAND_MAP.put("MDTM", new MDTM()); // rfc3659, 3
// "MFCT, draft-somers-ftp-mfxx, 4
// "MFF, draft-somers-ftp-mfxx, 5
// "MIC, rfc2228, 3?
DEFAULT_COMMAND_MAP.put("MFMT", new MFMT()); // draft-somers-ftp-mfxx, 3
DEFAULT_COMMAND_MAP.put("MKD", new MKD()); // rfc959, 4.1.3
DEFAULT_COMMAND_MAP.put("MLSD", new MLSD()); // rfc3659, 7
Expand Down Expand Up @@ -166,6 +166,7 @@ public class CommandFactoryFactory {

/**
* Create an {@link CommandFactory} based on the configuration on the factory.
*
* @return The {@link CommandFactory}
*/
public CommandFactory createCommandFactory() {
Expand Down Expand Up @@ -210,6 +211,7 @@ public Map<String, Command> getCommandMap() {

/**
* Add or override a command.
*
* @param commandName The command name, e.g. STOR
* @param command The command
*/
Expand Down
89 changes: 34 additions & 55 deletions core/src/main/java/org/apache/ftpserver/command/impl/PASS.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
* @author <a href="http://mina.apache.org">Apache MINA Project</a>
*/
public class PASS extends AbstractCommand {

private final Logger LOG = LoggerFactory.getLogger(PASS.class);

/**
Expand All @@ -64,46 +63,41 @@ public class PASS extends AbstractCommand {
public void execute(final FtpIoSession session,
final FtpServerContext context, final FtpRequest request)
throws IOException, FtpException {

boolean success = false;
ServerFtpStatistics stat = (ServerFtpStatistics) context .getFtpStatistics();

ServerFtpStatistics stat = (ServerFtpStatistics) context
.getFtpStatistics();
try {

// reset state variables
session.resetState();

// argument check
String password = request.getArgument();


// check user name
String userName = session.getUserArgument();

if (userName == null && session.getUser() == null) {
if ((userName == null) && (session.getUser() == null)) {
session.write(LocalizedFtpReply.translate(session, request, context,
FtpReply.REPLY_503_BAD_SEQUENCE_OF_COMMANDS, "PASS",
null));
FtpReply.REPLY_503_BAD_SEQUENCE_OF_COMMANDS, "PASS", null));

return;
}

// already logged-in
if (session.isLoggedIn()) {
session.write(LocalizedFtpReply.translate(session, request, context,
FtpReply.REPLY_202_COMMAND_NOT_IMPLEMENTED, "PASS",
null));
FtpReply.REPLY_202_COMMAND_NOT_IMPLEMENTED, "PASS", null));

return;
}

// anonymous login limit check
boolean anonymous = UserManager.ANONYMOUS.equals(userName);

boolean anonymous = userName != null
&& userName.equals("anonymous");
if (anonymous) {
int currAnonLogin = stat.getCurrentAnonymousLoginNumber();
int maxAnonLogin = context.getConnectionConfig()
.getMaxAnonymousLogins();
int maxAnonLogin = context.getConnectionConfig().getMaxAnonymousLogins();

if (maxAnonLogin == 0) {
LOG.debug("Currently {} anonymous users logged in, unlimited allowed", currAnonLogin);
} else {
Expand All @@ -112,58 +106,53 @@ public void execute(final FtpIoSession session,

if (currAnonLogin >= maxAnonLogin) {
LOG.debug("Too many anonymous users logged in, user will be disconnected");
session
.write(LocalizedFtpReply
.translate(
session,
request,
context,
session.write(LocalizedFtpReply.translate(session, request, context,
FtpReply.REPLY_421_SERVICE_NOT_AVAILABLE_CLOSING_CONTROL_CONNECTION,
"PASS.anonymous", null));

return;
}
}

// login limit check
int currLogin = stat.getCurrentLoginNumber();
int maxLogin = context.getConnectionConfig().getMaxLogins();

if (maxLogin == 0) {
LOG.debug("Currently {} users logged in, unlimited allowed", currLogin);
} else {
LOG.debug("Currently {} out of {} users logged in", currLogin, maxLogin);
}

if (maxLogin != 0 && currLogin >= maxLogin) {
LOG.debug("Too many users logged in, user will be disconnected");
session
.write(LocalizedFtpReply
.translate(
session,
request,
context,
session.write(LocalizedFtpReply.translate(
session, request, context,
FtpReply.REPLY_421_SERVICE_NOT_AVAILABLE_CLOSING_CONTROL_CONNECTION,
"PASS.login", null));

return;
}

// authenticate user
UserManager userManager = context.getUserManager();
User authenticatedUser = null;

try {
UserMetadata userMetadata = new UserMetadata();

if (session.getRemoteAddress() instanceof InetSocketAddress) {
userMetadata.setInetAddress(((InetSocketAddress) session
.getRemoteAddress()).getAddress());
userMetadata.setInetAddress(((InetSocketAddress) session.getRemoteAddress()).getAddress());
}
userMetadata.setCertificateChain(session
.getClientCertificates());

userMetadata.setCertificateChain(session.getClientCertificates());

Authentication auth;

if (anonymous) {
auth = new AnonymousAuthentication(userMetadata);
} else {
auth = new UsernamePasswordAuthentication(userName,
password, userMetadata);
auth = new UsernamePasswordAuthentication(userName, password, userMetadata);
}

authenticatedUser = userManager.authenticate(auth);
Expand All @@ -182,14 +171,9 @@ public void execute(final FtpIoSession session,

if (authenticatedUser != null) {
if (!authenticatedUser.getEnabled()) {
session
.write(LocalizedFtpReply
.translate(
session,
request,
context,
FtpReply.REPLY_530_NOT_LOGGED_IN,
"PASS", null));
session.write(LocalizedFtpReply.translate(
session, request, context, FtpReply.REPLY_530_NOT_LOGGED_IN, "PASS", null));

return;
}

Expand All @@ -207,8 +191,7 @@ public void execute(final FtpIoSession session,
session.setUserArgument(oldUserArgument);
session.setMaxIdleTime(oldMaxIdleTime);

delayAfterLoginFailure(context.getConnectionConfig()
.getLoginFailureDelay());
delayAfterLoginFailure(context.getConnectionConfig().getLoginFailureDelay());

LOG.warn("Login failure - " + userName);
session.write(LocalizedFtpReply.translate(session, request, context,
Expand All @@ -218,10 +201,9 @@ public void execute(final FtpIoSession session,
session.increaseFailedLogins();

// kick the user if the max number of failed logins is reached
int maxAllowedLoginFailues = context.getConnectionConfig()
.getMaxLoginFailures();
if (maxAllowedLoginFailues != 0
&& session.getFailedLogins() >= maxAllowedLoginFailues) {
int maxAllowedLoginFailues = context.getConnectionConfig().getMaxLoginFailures();

if ((maxAllowedLoginFailues != 0) && (session.getFailedLogins() >= maxAllowedLoginFailues)) {
LOG.warn("User exceeded the number of allowed failed logins, session will be closed");

session.close(false).awaitUninterruptibly(10000);
Expand All @@ -232,22 +214,20 @@ public void execute(final FtpIoSession session,

// update different objects
FileSystemFactory fmanager = context.getFileSystemManager();
FileSystemView fsview = fmanager
.createFileSystemView(authenticatedUser);
FileSystemView fsview = fmanager.createFileSystemView(authenticatedUser);
session.setLogin(fsview);
stat.setLogin(session);

// everything is fine - send login ok message
session.write(LocalizedFtpReply.translate(session, request, context,
FtpReply.REPLY_230_USER_LOGGED_IN, "PASS", userName));
FtpReply.REPLY_230_USER_LOGGED_IN, "PASS", userName));

if (anonymous) {
LOG.info("Anonymous login success - " + password);
LOG.info("Anonymous login success");
} else {
LOG.info("Login success - " + userName);
LOG.info("Login success - {}", userName);
}

} finally {

// if login failed - reset user
if (!success) {
session.reinitialize();
Expand All @@ -256,7 +236,6 @@ public void execute(final FtpIoSession session,
}

private void delayAfterLoginFailure(final int loginFailureDelay) {

if (loginFailureDelay > 0) {
LOG.debug("Waiting for {} milliseconds due to login failure", loginFailureDelay);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@
* @author <a href="http://mina.apache.org">Apache MINA Project</a>
*/
public class NativeFileSystemView implements FileSystemView {

private final Logger LOG = LoggerFactory
.getLogger(NativeFileSystemView.class);
private final Logger LOG = LoggerFactory.getLogger(NativeFileSystemView.class);


// the root directory will always end with '/'.
Expand Down Expand Up @@ -69,15 +67,15 @@ protected NativeFileSystemView(User user) throws FtpException {
*
* @param user The current user
* @param caseInsensitive If the OS FS is case sensitive or not
* @throws FtpException Actually, never thrown... To be removed!
*/
public NativeFileSystemView(User user, boolean caseInsensitive)
throws FtpException {
public NativeFileSystemView(User user, boolean caseInsensitive) throws FtpException {
if (user == null) {
throw new IllegalArgumentException("user can not be null");
}

if (user.getHomeDirectory() == null) {
throw new IllegalArgumentException(
"User home directory can not be null");
throw new IllegalArgumentException("User home directory can not be null");
}

this.caseInsensitive = caseInsensitive;
Expand All @@ -90,7 +88,6 @@ public NativeFileSystemView(User user, boolean caseInsensitive)
LOG.debug("Native filesystem view created for user \"{}\" with root \"{}\"", user.getName(), rootDir);

this.rootDir = rootDir;

this.user = user;

currDir = "/";
Expand All @@ -111,28 +108,28 @@ public FtpFile getHomeDirectory() {
*/
public FtpFile getWorkingDirectory() {
FtpFile fileObj = null;

if (currDir.equals("/")) {
fileObj = new NativeFtpFile("/", new File(rootDir), user);
} else {
File file = new File(rootDir, currDir.substring(1));
fileObj = new NativeFtpFile(currDir, file, user);

}

return fileObj;
}

/**
* {@inheritDoc}
*/
public FtpFile getFile(String file) {

// get actual file object
String physicalName = getPhysicalName(rootDir,
currDir, file, caseInsensitive);
String physicalName = getPhysicalName(rootDir, currDir, file, caseInsensitive);
File fileObj = new File(physicalName);

// strip the root directory and return
String userFileName = physicalName.substring(rootDir.length() - 1);

return new NativeFtpFile(userFileName, fileObj, user);
}

Expand Down Expand Up @@ -304,6 +301,7 @@ private String trimTrailingSlash(String path) {
private String normalizeSeparateChar(final String pathName) {
String normalizedPathName = pathName.replace(File.separatorChar, '/');
normalizedPathName = normalizedPathName.replace('\\', '/');

return normalizedPathName;
}

Expand Down
Loading

0 comments on commit 5ab035a

Please sign in to comment.