Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ The <action> type attribute can be add,update,fix,remove.
<action type="fix" dev="ggregory" due-to="Javid Khan, Gary Gregory">Force Gregorian calendar in FTP listing timestamp parsers (##406).</action>
<action type="fix" dev="ggregory" due-to="Arpit Jain, Gary Gregory">TFTPRequestPacket now throws TFTPPacketException instead of ArrayIndexOutOfBoundsException (#407).</action>
<!-- ADD -->
<action type="add" dev="ggregory" due-to="Inf1mum" issue="NET-746">Add FTPClient.login(String, char[]) so passwords are sent as char arrays and cleared after transmission; add FTP.pass(char[]) and FTP.sendCommand(String, char[]) / FTP.sendCommand(FTPCmd, char[]) overloads.</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add and use FTPFile.setUserPermission(int, boolean).</action>
<!-- UPDATE -->
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump org.apache.commons:commons-parent from 97 to 107.</action>
Expand Down
124 changes: 121 additions & 3 deletions src/main/java/org/apache/commons/net/ftp/FTP.java
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,32 @@ public class FTP extends SocketClient {

private static final String MODES = "AEILNTCFRPSBCZ";

/**
* Placeholder used in command-sent events fired for {@link #sendCommand(String, char[])} so that sensitive arguments are never materialized into a
* {@link String}.
*/
private static final String MASKED_ARGS = "******";

private static void checkCRLF(final String command, final String args) {
if (command != null && (command.indexOf('\r') >= 0 || command.indexOf('\n') >= 0)
|| args != null && (args.indexOf('\r') >= 0 || args.indexOf('\n') >= 0)) {
throw new IllegalArgumentException("Commands and arguments cannot contain CR or LF characters");
}
}

private static void checkCRLF(final String command, final char[] args) {
if (command != null && (command.indexOf('\r') >= 0 || command.indexOf('\n') >= 0)) {
throw new IllegalArgumentException("Commands and arguments cannot contain CR or LF characters");
}
if (args != null) {
for (final char c : args) {
if (c == '\r' || c == '\n') {
throw new IllegalArgumentException("Commands and arguments cannot contain CR or LF characters");
}
}
}
}

/**
* The last FTP reply code.
*/
Expand Down Expand Up @@ -282,7 +301,7 @@ protected void __getReplyNoReport() throws IOException {
* @since 3.0
*/
protected void __noop() throws IOException {
final String msg = buildMessage(FTPCmd.NOOP.getCommand(), null);
final String msg = buildMessage(FTPCmd.NOOP.getCommand(), (String) null);
send(msg);
__getReplyNoReport(); // This may time out
}
Expand Down Expand Up @@ -444,6 +463,23 @@ private String buildMessage(final String command, final String args) {
return builder.toString();
}

private char[] buildMessage(final String command, final char[] args) {
final int commandLen = command.length();
final int eolLen = NETASCII_EOL.length();
final boolean hasArgs = args != null;
final char[] message = new char[commandLen + (hasArgs ? args.length + 1 : 0) + eolLen];
int pos = 0;
command.getChars(0, commandLen, message, pos);
pos += commandLen;
if (hasArgs) {
message[pos++] = SP;
System.arraycopy(args, 0, message, pos, args.length);
pos += args.length;
}
NETASCII_EOL.getChars(0, eolLen, message, pos);
return message;
}

/**
* Sends the FTP {@code CDUP} command to the server, receive the reply, and return the reply code.
*
Expand Down Expand Up @@ -1048,6 +1084,25 @@ public int pass(final String password) throws IOException {
return sendCommand(FTPCmd.PASS, password);
}

/**
* Sends the FTP {@code PASS} command to the server, receive the reply, and return the reply code.
* <p>
* The password is provided as a {@code char} array so that it is never materialized into a {@link String}, which cannot be reliably wiped. Callers are
* responsible for clearing the array afterwards, see {@link FTPClient#login(String, char[])}.
* </p>
*
* @param password The plain text password of the user being logged into.
* @return The reply code received from the server.
* @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
* causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
* independently as itself.
* @throws IOException If an I/O error occurs while either sending the command or receiving the server reply.
* @since 3.13.1
*/
public int pass(final char[] password) throws IOException {
return sendCommand(FTPCmd.PASS, password);
}

/**
* Sends the FTP {@code PASV} command to the server, receive the reply, and return the reply code. Remember, it's up to you to interpret the
* reply string containing the host/port information.
Expand Down Expand Up @@ -1209,6 +1264,18 @@ private void send(final String message) throws IOException, FTPConnectionClosedE
}
}

private void send(final char[] message) throws IOException, FTPConnectionClosedException, SocketException {
try {
_controlOutput_.write(message);
_controlOutput_.flush();
} catch (final SocketException e) {
if (!isConnected()) {
throw new FTPConnectionClosedException("Connection unexpectedly closed.");
}
throw e;
}
}

/**
* Sends an FTP command to the server, waits for a reply and returns the numerical response code. After invocation, for more detailed information, the
* actual reply text can be accessed by calling {@link #getReplyString getReplyString} or {@link #getReplyStrings getReplyStrings}.
Expand All @@ -1222,7 +1289,7 @@ private void send(final String message) throws IOException, FTPConnectionClosedE
* @since 3.3
*/
public int sendCommand(final FTPCmd command) throws IOException {
return sendCommand(command, null);
return sendCommand(command, (String) null);
}

/**
Expand All @@ -1242,6 +1309,28 @@ public int sendCommand(final FTPCmd command, final String args) throws IOExcepti
return sendCommand(command.getCommand(), args);
}

/**
* Sends an FTP command to the server, waits for a reply and returns the numerical response code. After invocation, for more detailed information, the
* actual reply text can be accessed by calling {@link #getReplyString getReplyString} or {@link #getReplyStrings getReplyStrings}.
* <p>
* The arguments are provided as a {@code char} array so that sensitive data (like passwords) is never materialized into a {@link String}, which cannot
* be reliably wiped. Unlike {@link #sendCommand(FTPCmd, String)}, the {@code ProtocolCommandEvent} fired to command listeners does not contain the
* command arguments, they are reported as masked.
* </p>
*
* @param command The FTPCmd enum corresponding to the FTP command to send.
* @param args The arguments to the FTP command. If this parameter is set to null, then the command is sent with no argument.
* @return The integer value of the FTP reply code returned by the server in response to the command.
* @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
* causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
* independently as itself.
* @throws IOException If an I/O error occurs while either sending the command or receiving the server reply.
* @since 3.13.1
*/
public int sendCommand(final FTPCmd command, final char[] args) throws IOException {
return sendCommand(command.getCommand(), args);
}

/**
* Sends an FTP command with no arguments to the server, waits for a reply and returns the numerical response code. After invocation, for more detailed
* information, the actual reply text can be accessed by calling {@link #getReplyString getReplyString} or {@link #getReplyStrings getReplyStrings}.
Expand Down Expand Up @@ -1287,7 +1376,7 @@ public int sendCommand(final int command, final String args) throws IOException
* @throws IOException If an I/O error occurs while either sending the command or receiving the server reply.
*/
public int sendCommand(final String command) throws IOException {
return sendCommand(command, null);
return sendCommand(command, (String) null);
}

/**
Expand All @@ -1313,6 +1402,35 @@ public int sendCommand(final String command, final String args) throws IOExcepti
return getReply();
}

/**
* Sends an FTP command to the server, waits for a reply and returns the numerical response code. After invocation, for more detailed information, the
* actual reply text can be accessed by calling {@link #getReplyString getReplyString} or {@link #getReplyStrings getReplyStrings}.
* <p>
* The arguments are provided as a {@code char} array so that sensitive data (like passwords) is never materialized into a {@link String}, which cannot
* be reliably wiped: the command is built and written to the control connection as {@code char} arrays only. Unlike
* {@link #sendCommand(String, String)}, the {@code ProtocolCommandEvent} fired to command listeners does not contain the command arguments, they are
* reported as masked.
* </p>
*
* @param command The text representation of the FTP command to send.
* @param args The arguments to the FTP command. If this parameter is set to null, then the command is sent with no argument.
* @return The integer value of the FTP reply code returned by the server in response to the command.
* @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
* causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
* independently as itself.
* @throws IOException If an I/O error occurs while either sending the command or receiving the server reply.
* @since 3.13.1
*/
public int sendCommand(final String command, final char[] args) throws IOException {
checkCRLF(command, args);
if (_controlOutput_ == null) {
throw new IOException("Connection is not open");
}
send(buildMessage(command, args));
fireCommandSent(command, buildMessage(command, args != null ? MASKED_ARGS : null));
return getReply();
}

/**
* Sets the character encoding to be used by the FTP control connection. Some FTP servers require that commands be issued in a non-ASCII encoding like
* UTF-8 so that file names with multi-byte character representations (e.g, Big 8) can be specified.
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/org/apache/commons/net/ftp/FTPClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -2435,6 +2436,41 @@ public boolean login(final String user, final String password) throws IOExceptio
return FTPReply.isPositiveCompletion(pass(password));
}

/**
* Login to the FTP server using the provided user and password.
* <p>
* The password is provided as a {@code char} array so that it is never materialized into a {@link String}, which cannot be reliably wiped. Once the
* password has been transmitted to the server, the array is cleared with {@link Arrays#fill(char[], char)}; note that this happens even if the server
* rejects the password, so the same array cannot be reused for a second login attempt.
* </p>
*
* @param user The user name to login under.
* @param password The password to use. It is cleared after being sent to the server.
* @return True if successfully completed, false if not.
* @throws FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason
* causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
* independently as itself.
* @throws IOException If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
* @since 3.13.1
*/
public boolean login(final String user, final char[] password) throws IOException {
user(user);
if (FTPReply.isPositiveCompletion(_replyCode)) {
return true;
}
// If we get here, we either have an error code, or an intermediate
// reply requesting password.
if (!FTPReply.isPositiveIntermediate(_replyCode)) {
return false;
}
try {
return FTPReply.isPositiveCompletion(pass(password));
} finally {
// The password has been handed to the control connection: wipe it.
Arrays.fill(password, (char) 0);
}
}

/**
* Login to the FTP server using the provided username, password, and account. If no account is required by the server, only the username and password, the
* account information is not used.
Expand Down
25 changes: 24 additions & 1 deletion src/main/java/org/apache/commons/net/ftp/FTPSClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -959,7 +959,30 @@ public long parsePBSZ(final long pbsz) throws SSLException, IOException {
// Would like to remove this method, but that will break any existing clients that are using CCC
@Override
public int sendCommand(final String command, final String args) throws IOException {
final int repCode = super.sendCommand(command, args);
return postSendCCC(super.sendCommand(command, args), command);
}

/**
* Send an FTP command with sensitive arguments. A successful CCC (Clear Command Channel) command causes the underlying {@link SSLSocket} instance to be
* assigned to a plain {@link Socket}
*
* @param command The FTP command.
* @param args The arguments to the FTP command as a {@code char} array, for sensitive data.
* @return server reply.
* @throws IOException If an I/O error occurs while sending the command.
* @throws SSLException Thrown if a CCC command fails
* @see org.apache.commons.net.ftp.FTP#sendCommand(String, char[])
* @since 3.13.1
*/
@Override
public int sendCommand(final String command, final char[] args) throws IOException {
return postSendCCC(super.sendCommand(command, args), command);
}

/**
* Handles the clear command channel transition after a {@code CCC} command was sent.
*/
private int postSendCCC(final int repCode, final String command) throws IOException {
/* If CCC is issued, restore socket i/o streams to unsecured versions */
if (CMD_CCC.equals(command)) {
if (FTPReply.COMMAND_OK != repCode) {
Expand Down
Loading