How to use SFTP in Delphi?

Discussion of open issues, suggestions and bugs regarding network security and data protection solution - SecureBridge
Post Reply
karmacomposer
Posts: 11
Joined: Mon 02 Aug 2021 21:57

How to use SFTP in Delphi?

Post by karmacomposer » Fri 08 Oct 2021 17:40

Thank you. We are now using Delphi Rad Studio Architect.

We bought securebridge, so we are using the commercial licensed version. I have yet to get anything working.

The above documentation links give 404 errors.

I figured out how to change the port, but I am getting a SHA error when I try to connect.

This is all very complex for a simple procedure. I just want to send and receive files through an ssh tunnel (hence the sftp).

I do not understand the relationship between the filestore, ssh client and sftp client. I know you need all 3 on the form, but how do you set them up to SFTP a file?

Right now, I have this code:

Code: Select all

ScFileStorage := TScFileStorage.Create(nil);
ScSSHClient := TScSSHClient.Create(nil);
ScSSHClient.KeyStorage := ScFileStorage;
ScSSHClient.Port := 522;
//ScSSHClient.OnServerKeyValidate := ScSSHClientServerKeyValidate;
ScSSHClient.HostName := serverHostName;
ScSSHClient.User := serverUsername;
ScSSHClient.Password := serverPassword;
ScSSHClient.Connect;

ScSFTPClient := TScSFTPClient.Create(nil);
ScSFTPClient.SSHClient := ScSSHClient;
ScSFTPClient.Initialize;
ScSFTPClient1.UploadFile(varUploadFile, varServerFile, True);
//ScSFTPClient.DownloadFile(varDownloadFile, varDownloadFile, False);
ScSSHClient.Disconnect;
When I tried to include this, it gave me an undeclared identifier error for ScSSHClientServerKeyValidate:

Code: Select all

procedure TfrmRubidexCoreTechDemo.ScSSHClientServerKeyValidate(Sender: TObject; NewServerKey: TScKey; var Accept: Boolean);
begin
  Accept := True;
end;
So I deleted it.

As a result, I commented out this line because it gave me errors:

//ScSSHClient.OnServerKeyValidate := ScSSHClientServerKeyValidate;

I get a sha256 error when I run this (yes, the port is correct). The variables (serverHostName, serverUsername, serverPassword, etc) are filled in above this code and are correct.

I have not defined any keys because I do not understand that part. I do not understand how a server should have a key and then it should match in the program. In other languages, I would just use ssh commands and they would just work. Yours seems to require too much configuration just to get started.

Is there an easy way to simply send and receive files through ssh?

Mike

YanishevskiyVI
Devart Team
Posts: 70
Joined: Wed 02 Jun 2021 09:30

Re: How to use SFTP in Delphi?

Post by YanishevskiyVI » Wed 20 Oct 2021 13:52

Hi Mike,
karmacomposer wrote: The above documentation links give 404 errors.
Yes that's right we have expirience some technical difficulties, here is a correct link

Code: Select all

https://www.devart.com/sbridge/docs/
Please be informed, we provide offline help, included into a distributive. You may use in in a general way, by pressing F1 (WinHlp32 need to be installed)
karmacomposer wrote:I do not understand the relationship between the filestore, ssh client and sftp client. I know you need all 3 on the form, but how do you set them up to SFTP a file?
Yes, they are necessary.
  • TScSSHClient create and maintain SSH connection
  • TScFileStorage used for connection data storing (such as keys and certificates, but NOT files from remote file system, don't be confused!) it is mandatory for TScSSHClient.
  • TScSFTPClient perform file operations thru secure connection, created by TScSSHClient.
karmacomposer wrote: Right now, I have this code:

Code: Select all

...
When I tried to include this, it gave me an undeclared identifier error for ScSSHClientServerKeyValidate:
Please add ScBridge to your uses clause, it must help. Other portion of code is perfect.
karmacomposer wrote:I have not defined any keys because I do not understand that part. I do not understand how a server should have a key and then it should match in the program. In other languages, I would just use ssh commands and they would just work. Yours seems to require too much configuration just to get started.
Other languages uses external libraries, and executing step-by-step by series of calls. TScSSHClient perform desired actions automatically, thus, previously need to be properly configured. That's a difference.

A typical connection scenario looks like this:
  • TScSSHClient connects to Server;
  • The Server sends its Key;
  • The TScSSHClient performs Validation of this Key: searches in the TScStorage, and if it is not there, generates a OnServerKeyValidation event, where it is necessary to decide whether to accept the Key or not (this is what the event is for, so implementing as Accept := True it's not a good idea);
  • If the Key is accepted, it is storing into a TScStorage (thus next time stored Key will be found and OnServerKeyValidation will not occur) and the connection is established;
  • Now TScSFTPClient is able to perform file operations on the Server.
Regards,
Vitaliy

Post Reply