JAVA FTPSClient file listing issue - java

I am using apache commons-net 3.6 library to connect FTPS server. FTPS server is behind NAT of thirdparty. and I can't change any settings on server side.
I can login to server, but can not list files. I've tried same code with some public FTP and FTPS servers, and result was successfull. Seems that they are not behind NAT. But filezilla can successfully connect and list files from my problematic server.
There is my code
ftps.connect(server, port);
System.out.println("Connected to " + server + ".");
reply = ftps.getReplyCode();
ftps.enterLocalPassiveMode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftps.disconnect();
System.err.println("FTP server refused connection.");
System.exit(1);
}
if (!ftps.login(username, password)) {
ftps.logout();
}
// ftps.setEnabledSessionCreation(false);
ftps.feat();
ftps.execPBSZ(0);
ftps.execPROT("P");
ftps.setFileType(FTP.BINARY_FILE_TYPE);
FTPFile dirs[] = ftps.listDirectories();
And there is my ftps log:
220 FTP Server ready.
AUTH TLS
234 AUTH TLS successful
Connected to x.x.x.x
USER *******
331 Password required for azercell
PASS *******
230 User myuser logged in
FEAT
211-Features:
MDTM
MFMT
LANG bg-BG;en-US;fr-FR;it-IT;ja-JP;ko-KR;ru-RU;zh-CN;zh-TW
TVFS
UTF8
AUTH TLS
MFF modify;UNIX.group;UNIX.mode;
MLST modify*;perm*;size*;type*;unique*;UNIX.group*;UNIX.mode*;UNIX.owner*;
PBSZ
PROT
REST STREAM
SIZE
211 End
PBSZ 0
200 PBSZ 0 successful
PROT P
200 Protection set to Private
TYPE I
200 Type set to I
SYST
215 UNIX Type: L8
PASV
227 Entering Passive Mode (192,168,2,50,192,12).
[Replacing PASV mode reply address 192.168.2.50 with x.x.x.x]
LIST
150 Opening BINARY mode data connection for file list
425 Unable to build data connection: Operation not permitted
I'd read that prior to version 3.6 commons-net library prior couldnt handle behind NAT connections properly.
Can anyone help me? What is wrong with my code?

So my conclusion is problem was not related to NAT technology, apache-commons 3.6 does not handle all FTPS options properly. As I mentioned before we were integrating with 3rd party and had not option to change FTPS settings, at least we installed filezilla ftp server and were able to reproduce error. Fortunately I found solution at http://eng.wealthfront.com/2016/06/10/connecting-to-an-ftps-server-with-ssl-session-reuse-in-java-7-and-8/ by Luke Hansen. Great thanks him

Related

425 Failed to establish connection

I'm trying to download a file via FTP with a Java application.
The FTP url is accessible from this web page: http://professionnels.ign.fr/adminexpress.
More specifically, I'm trying to download this file.
From my home, I can download the file successfully with my java application, Firefox or Chrome.
From my work, I can do the same with Firefox and Chrome only. My application refuses to download anything.
NOTA: At work, the browsers and my application use the same HTTP proxy to access internet.
I'm using Apache Commons Net 3.6.
Here is a sample of the FTP exchanges of my application. I wasn't able to sniff those of Chrome or Firefox.
220 Bienvenue sur le site FTP de L INSTITUT NATIONAL DE L INFORMATION GEOGRAPHIQUE ET FORESTIERE
USER *******
331 Please specify the password.
PASS *******
230 Login successful.
TYPE I
200 Switching to Binary mode.
PASV
227 Entering Passive Mode (192,134,132,16,65,180).
RETR /ADMIN-EXPRESS-COG_2-0__SHP_WM__FRA_2019-05-20.7z.001
425 Failed to establish connection.
tl;dr
It turned out that the HTTP proxy at my work already handles all the FTP exchanges. This is why Firefox and Chrome could download the file. When they aren't behind an HTTP proxy, it seems they act as an FTP client by sending FTP commands directly.
A simple HTTP GET request to the HTTP proxy with the ftp url is enough to download the file.
Here is a sum up of solutions I found during my investigations:
Use passive mode (PASV command)
Check if there's an FTP proxy to use rather than an HTTP Proxy
Check the configuration of the FTP server (if you have access to it)
Check the configuration of the HTTP proxy (if you have access to it)
Precisely, the browsers perform a simple HTTP request as described below:
GET ftp://user:passw0rd#example.com/file.ext HTTP/1.1
Host: example.com
User-Agent: WebBrowser-UA/x.y.z
...
Then the HTTP proxy parses the FTP url and connects to the FTP server. The HTTP proxy returns the file content as a normal HTTP response.
HTTP/1.1 200 OK
Last-Modified: Tue, 21 May 2019 11:23:00 GMT
Content-Length: 115545060
Content-Type: octet/stream
Connection: Keep-Alive
Age: 22
Date: Thu, 27 Jun 2019 10:27:09 GMT
(file content here...)
However, in my case, the HTTP proxy allowed me to connect to the FTP server and exchange on the command FTP channel only. The data channel seemed to be blocked either in ACTIVE or PASSIVE mode.
During my investigations, I found many people hitting this very same problem. The solutions they found (when they found one...) didn't apply to me. Here is a sum up of the solutions expressed in all those questions:
Use passive mode (PASV command)
Check if there's an FTP proxy to use rather than an HTTP Proxy
Check if the HTTP proxy handles directly the FTP exchanges
Check the configuration of the FTP server (if you have access to it)
Check the configuration of the HTTP proxy (if you have access to it)
References:
Understanding FTP over HTTP
Connect to FTP server through http proxy
FTP connection through proxy with Java
Accessing FTP server behind a proxy via command prompt in Windows 7
[vsFTPd] 425 Failed to establish connection.

How to access apache mina ssh server from remote machine?

I have created an Amazon Linux 2 instance where I have deployed a Java program launched with systemd. The Java program is a vertx-shell application which is using Apache Mina to start a SSH server on port 2000. It should possible to connect to the SSH server with 2 ways : public key or password auth.
After a classical ssh authentication on port 22 to access my amazon instance, I can connect to the java ssh server running on port 2000 locally with password auth. However, when I try to connect to SSH server from my local machine providing the Amazon private key, the connection is stuck on 'debug1: Local version string SSH-2.0-OpenSSH_7.5' line and finally refused after a timeout of 2 minutes :
ssh -i /Users/toto/.ssh/my_amazon_key.pem -p 2000 ec2-user#my-application.server.com -vvv
OpenSSH_7.5p1, LibreSSL 2.5.4
debug1: Reading configuration data /Users/toto/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 52: Applying options for *
debug2: resolving "my-application.server.com" port 2000
debug2: ssh_connect_direct: needpriv 0
debug1: Connecting to my-application.server.com [35.XXX.XX.XX] port 2000.
debug1: Connection established.
debug1: key_load_public: No such file or directory
debug1: identity file /Users/toto/.ssh/my_amazon_key.pem type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/toto/.ssh/my_amazon_key.pem-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.5
ssh_exchange_identification: Connection closed by remote host
On the Java application log, I can see a log of somebody trying to connect to the SSH server but failing to authenticate after 2 minutes.
Of course, I have checked in my Amazon security group that I have opened port 2000. There is no denied IP in my case.
Here is the Java code running the SSH server :
ShellService service = ShellService.create(vertx,
new ShellServiceOptions()
.setTelnetOptions(
new TelnetTermOptions().
setHost("localhost").
setPort(5000))
.setSSHOptions(
new SSHTermOptions().
setHost("0.0.0.0").
setPort(2000).
setKeyPairOptions(new JksOptions().
setPath("ssh/keystore.jks").
setPassword("wibble")).
setAuthOptions(new ShiroAuthOptions().
setConfig(new JsonObject().put("properties_path", "classpath:ssh/auth.properties"))))
);
Any idea ? Vertx/Apache Mina config ? Conflict between SSHD running on port 22 and port 2000 ?
Ok, I finally found out the problem. Actually, I had to do 2 things :
I changed the port from 2000 to 3000, and then I was not stuck anymore, at least I was able to authenticate with password. But public key authentication was still failing. So I suppose something else was already running on initial port 2000, causing the connection stuck.
I was launching my java application with a specific user vertx, so Apache Mina SSH server was looking for an un-existing authorized_keys file for user vertx. As soon as I created it with the public key inside in its own .ssh directory in vertx user home directory, with the right permissions, then I could authenticate providing the corresponding private key.

java: getting Connection refused when trying to upload file to FTP

When trying to upload file to FTP with java program:
public void upload(String localFile,String remoteFile) throws Exception{
ftp = new FTPClient();
ftp.setControlKeepAliveTimeout(300);
ftp.connect(host,21);
ftp.enterLocalPassiveMode();
ftp.setUseEPSVwithIPv4(false);
ftp.login(user,password);
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
FileInputStream in = null;
in = new FileInputStream(localFile);
ftp.storeFile(remoteFile,in);
in.close();
ftp.disconnect();
}
I'm getting:
java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:381)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:243)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:230)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:377)
at java.net.Socket.connect(Socket.java:539)
at org.apache.commons.net.SocketClient._connect(SocketClient.java:243)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:202)
When I try to upload the same file with command line (from linux), I'm able to do it only when using EPSV:
llnx:~ ftp anonymous#9.20.1.116
Connected to 9.20.1.116.
220 Microsoft FTP Service
331 Anonymous access allowed, send identity (e-mail name) as password.
Password:
230 User logged in.
Remote system type is Windows_NT.
ftp> epsv
EPSV/EPRT on IPv4 off.
ftp> put /tmp/file1.xml /dir_1/file1.xml
local: /tmp/file1.xml remote: /dir_1/file1.xml
227 Entering Passive Mode (10,40,1,149,233,168).
125 Data connection already open; Transfer starting.
100% |*************************************| 117 KB 28.66 MB/s --:-- ETA
226 Transfer complete.
120032 bytes sent in 00:00 (7.96 MB/s)
So, Why does my java code getting Connection refused?
Maybe I'm not using the enterLocalPassiveMode() or setUseEPSVwithIPv4() method the right way?
*** I think the answer is how to run the EPSV command from Java program.
Thank you all.
Eithan.
This is purely a guess but java.net.ConnectException: Connection refused normally happens when there is nothing listening on the target host/port. You don't specify a port in the CLI example so maybe that is the problem. Try changing ftp.connect(host,21); to ftp.connect(host); to use the default. Also confirm the the hostnames are exactly the same.
This assumes that the error is on the call to connect(). You haven't provided a big enough stack trace to indicate either way.
Connection refused means that your TCP connection request has reached the remote server (or more correctly >>a<< remote server) but the server is not expecting / listening for an incoming connection. So it "refuses" it.
Here are the things to check:
Check that you have the correct remote hostname or IP address for the FTP server.
Check that you are using the correct port for the FTP server. Port 21 is the default, but it is possible that the server is on a non-standard port.
Check that the FTP server is actually running.
It is also possible that the problem is due to a firewall doing something deliberately confusing. But that is unlikely for a publicly routable FTP server.
Maybe I'm not using the enterLocalPassiveMode() or setUseEPSVwithIPv4() method the right way?
That can't be the problem. The stacktrace shows that your application failed while trying to establish the initial connection to the server. You haven't gotten to the point where the you can make those calls.

sendmail SMTP connection timed out error at MATLAB

Kindly, I have the mentioned error for this code when running on Wifi while it works when using phone 3G hotspot:
username='XXX#hotmail.com';
pass='XXXXX';
port='587'; % hotmail
props = java.lang.System.getProperties;
props.setProperty('mail.smtp.starttls.enable', 'true' );
props.setProperty('mail.smtp.auth','true');
props.setProperty('mail.smtp.socketFactory.port',port);
setpref('Internet','SMTP_Server','smtp.live.com');
setpref('Internet','E_mail',username);
setpref('Internet','SMTP_Username',username);
setpref('Internet','SMTP_Password',pass);
sendmail(username,'Available');
Error:
Error using sendmail (line 171)
Could not connect to SMTP host: smtp.live.com, port: 25;
Connection timed out: connect
Should I change anything in the router settings?
enter image description here
You might have port 25 blocked. Try this:
telnet smtp.live.com 25
and copy results here.
Port 25 is often blocked by ISP to prevent spamming.
If you use Windows OS, you may have telnet client switched off in your system, so you are not able to use it in your command window (cmd). Go to Control Panel -> Programs and Features -> Turn Windows Features on or off and check Telnet Client.

Apache Commons Net FTPClient Will Not Execute listFiles()

I'm modifying some code that was previously working with an FTPS library I wrote myself. I've been asked to start using the Apache Commons Net library (FTPClient and FTPSClient mainly) and I'm running into problems doing a file listing. I've read other questions and it's not the enterLocalPassiveMode problem (Apache Commons Net FTPClient and listFiles()), as I'm using that after connecting, but before logging in. The same code works fine on a test server I set up (also using Apache FTP), but doesn't work on the server I need it for.
I've also tried using the "PBSZ 0" and "PROT P" commands, but they're not implemented on the remote system.
502 PBSZ Command not implemented.
502 PROT Command not implemented.
Code:
FTPSClient ftpsclient = new FTPSClient(true); // Implicit SSL
ftpsclient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
ftpsclient.connect(HOST_ADDR, HOST_PORT); // Using port 990
ftpsclient.enterLocalPassiveMode();
ftpsclient.user(USERID);
ftpsclient.pass(PASSWORD);
ftpsclient.setFileType(FTP.BINARY_FILE_TYPE);
ftpsclient.changeWorkingDirectory(REMOTE_DIR);
ftpsclient.printWorkingDirectory();
FTPFile[] ftpfiles = ftpsclient.listFiles(); // This is where it breaks
I've tried specifying the directory explicitly and using the default:
FTPFile[] ftpfiles = ftpsclient.listFiles();
FTPFile[] ftpfiles = ftpsclient.listFiles(REMOTE_DIR);
... but both give the same result. This is the output of the debugging info:
220 FTPS (Version Thu Dec 10 17:23:00 2015) server ready.
USER ****
331 Password required for ****.
PASS ****
230 User **** logged in.
TYPE I
200 Type set to I
CWD outbound/directory
250 CWD Command successful.
PWD
257 "/usr/path/to/outbound/directory" is current directory.
SYST
215 UNIX
PASV
227 Entering Passive Mode (XX,XX,XX,XX,24,140) ***Edit: port 6284
LIST
150 Opening data connection for '/bin/ls'.
Then it times out after 30 seconds with this stack trace:
Stack Trace: org.apache.commons.net.ftp.FTPConnectionClosedException: Connection closed without indication.
at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:317)
at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:294)
at org.apache.commons.net.ftp.FTP.getReply(FTP.java:692)
at org.apache.commons.net.ftp.FTPClient.completePendingCommand(FTPClient.java:1813)
at org.apache.commons.net.ftp.FTPClient.initiateListParsing(FTPClient.java:3308)
at org.apache.commons.net.ftp.FTPClient.initiateListParsing(FTPClient.java:3271)
at org.apache.commons.net.ftp.FTPClient.listFiles(FTPClient.java:2930)
I've checked my firewall settings and that host is allowed to connect via port 990 and 6200-6300.
I've also read FTPClient.listFiles not working and Java application hanging during LIST command to FTP Server (Apache Commons FTPClient) and neither of these have helped with my problem.
EDIT: It looks as though FTPClient is not recognizing the data channel. I tried uploading a file instead of doing a listing, and it died after the "150 Opening data connection" message. I've confirmed that the ports assigned to the data connection from the PASV command are NOT blocked by our firewall.
Any ideas?
I fixed it. Turns out I was doing a directory listing with an invalid path. Instead of returning an error message, Apache FTP closed the connection. Not sure why. Anyway, it's working now.

Categories