0

I have asked this question at the FPC forums (here) but answers are not forthcoming so I hoped someone here could help.

I have an Apple Mac server that has FTP enabled via SSH connection using port 22. I can connect to it fine using the FireFTP Firefox FTP plugin by specifying 'sFTP' in the encryption setting. I have another server (Windows) that I can connect to using "Implict SSL (Good)" and port 990 which I think is FTPS or similar. I can connect to that too, using FireFTP.

I have created a small demo project to see if I can connect using my own application. I have the Synpase library and the two DLL's (libssl32.dll libeay32.dll) that are needed for secure connections in my project. All compiles. But I just cannot get it to connect. The code is below. When I connect to these same FTP servers using FireFTP, I am asked if I want to accept and store the certificate. Could this be the problem? My code has no way to say "User, do you want to accept the certificate?". Is there a way to achieve this? In Delphi, such connections seem easy (http://www.example-code.com/delphi/ftp_ImplicitSSL.asp) but I am really struggling with what I had assumed would be an easy task.

unit Unit1;

{$mode objfpc}{$H+}

interface

uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
ftpsend, ssl_openssl; // From Synapse library

type

{ TForm1 }

TForm1 = class(TForm)
Button1: TButton;
ListBox1: TListBox;
procedure Button1Click(Sender: TObject);

function SendFTP(Host, Username, Password : string) : boolean;
private
{ private declarations }
public
{ public declarations }
end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
begin
  if SendFTP('My.IP.Address.123', 'FTPUserAcc', 'FTPPassword') then ShowMessage('Connected');
end;

// To just test if we can login to the server, for now. Expand once that is working
//  From http://forum.lazarus.freepascal.org/index.php?topic=20061.0

function TForm1.SendFTP(Host, Username, Password : string) : boolean;
var
FTP: TFTPSend;
begin
  FTP := TFTPSend.Create;
  try
    try
      FTP.TargetHost := Host;
      FTP.TargetPort := '990'; // For Implict SSL, 22 for standard SSH
      FTP.AutoTLS := true; // also tried FTP.FullSSL
      FTP.UserName   := Username;
      FTP.Password   := Password;
      FTP.Login;
    except
    on E: Exception do
      begin
        Showmessage('Exception: '+E.Message);
      Exit;
    end;
  end;

  FTP.Logout;
finally
  FTP.Free;
  end;
end;
//=============================================================================

end.
Thom A
  • 88,727
  • 11
  • 45
  • 75
Gizmo_the_Great
  • 979
  • 13
  • 28

2 Answers2

2

For others...

sFTP Synapse example can be found buried away at http://synapse.ararat.cz/files/contrib/sftp.zip (other examples are http://synapse.ararat.cz/files/contrib/)

The Pascal version of the CryptLib unit, that is needed and called as a uses requirement by one part of the sFTP example, and that is not supplied with Synapse itself, can be downloaded from http://cryptlib.sogot.de/crypas.html. Download the zip file and extract the cryptlib.pas file to your project or your Synapse folder.

The full version of CryptLib can be found here https://www.cs.auckland.ac.nz/~pgut001/cryptlib/

In theory, all manner of secure FTP connections can now be achieved.

I hope that helps others avoid the pain I have endured!

Gizmo_the_Great
  • 979
  • 13
  • 28
1

As @slim commented, you are connecting with FTPS (FTP over TLS) protocol to an SFTP server. These are two completely different and incompatible protocols.

You needs to use an SFTP client library. See FTP Over SSH (SFTP) In delphi 2010.

Community
  • 1
  • 1
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • Thanks, both of you. I probably wasn't clear but yes, I know they are two different protocols. My question was how to connect using one OR the other. I wasn't trying to connect using both. The solution that Martin posted I have seen before but there seems to be a problem with the cryptlib unit that is called in the ssl_cryptlib.pas file. It is causing me too much of a headache so I have, for now, resorted to plain port 21 anonymous connection. Not ideal but it is only a proof of concept idea I am trying out anyway. I had just assumed that an sFTP connection or similar would be easier these day – Gizmo_the_Great Feb 25 '15 at 21:26