u123
u123

Reputation: 16267

Unable to connect to remote FileZilla ftp server from java application

I have a remote FileZilla ftp server running on a windows machine. The ftp server requires Explicit FTP over TLS. The protocol is FTP and NOT SFTP. I cannot change the settings for this server. I can connect to this server using the filezilla gui client just fine.

Now I need to connect to the FileZilla server through a java application using org.apache.commons.net:

  private void connect(String host, String user, String password) {
    try {
      FTPSClient ftpClient = new FTPSClient(false);
      ftpClient.connect(host);
      int reply = ftpClient.getReplyCode();
      if (FTPReply.isPositiveCompletion(reply)) {
        // Login
        if (ftpClient.login(user, password)) {

          // Set protection buffer size
          ftpClient.execPBSZ(0);
          // Set data channel protection to private
          ftpClient.execPROT("P");
          // Enter local passive mode
          ftpClient.enterLocalPassiveMode();
          ftpClient.logout();
        } else {
          System.out.println("FTP login failed");
        }
        // Disconnect
        ftpClient.disconnect();
      } else {
        System.out.println("FTP connect to host failed");
      }
    } catch (IOException ioe) {
      ioe.printStackTrace();
      System.out.println("FTP client received network error");
    }
  }

but when I run the above code I get:

javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateExpiredException: NotAfter: Thu Aug 30 13:31:23 CEST 2012
    at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:174)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1764)

when it comes to:

  ftpClient.connect(host);

Any ideas on how to connect to a Filezilla server from java code using eg. org.apache.commons.net ?

EDIT: I have now tried to change to FTPClient (even though that does allow me to setup Explicit TLS):

  FTPClient ftpClient = new FTPClient();
  // Connect to host
  ftpClient.connect(host);
  int reply = ftpClient.getReplyCode();
  if (FTPReply.isPositiveCompletion(reply)) {

    // Login
    boolean login = ftpClient.login(user, password);
    if (login) {
      ftpClient.enterLocalPassiveMode();
      ftpClient.logout();
    } else {
      System.out.println("FTP login failed");
    }

but then login=false and I get: "FTP login failed". And if I debug the apache source I see the reply code is: 530 = "Not logged in" : http://en.wikipedia.org/wiki/List_of_FTP_server_return_codes

Upvotes: 1

Views: 3863

Answers (1)

u123
u123

Reputation: 16267

Creating a SSLContext solved the problem:

  SSLContext sslContext = SSLContext.getInstance("TLS");
  TrustManager tm = new X509TrustManager() {
    public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
    }

    public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
    }

    public X509Certificate[] getAcceptedIssuers() {
      return null;
    }
  };
  sslContext.init(null, new TrustManager[] { tm }, null);
  FTPSClient ftpsClient = new FTPSClient(sslContext);

Upvotes: 2

Related Questions