Contents · Forum Home · StreamSec Tools 4.1.5.365 documentation

Run an SFTP server

Give an SSH account access to a dedicated directory. The client sees this directory as / and signs in before it can list or transfer files.

The components on this page are normally placed on a form or a data module at design time and set up in the Object Inspector. The sample creates them in code instead, because code shows at a glance which properties are changed from their default values.

Configure the SSH listener

Use a fresh SSH server, SFTP subsystem and user list. Keep the components and the host key alive for as long as the server is active.

uses
  System.SysUtils, System.IOUtils, stSHA256, stAES, stPkcs5, stDrbgAesCtr,
  StreamSec.DSI.Arith, StreamSec.DSI.ECArith, StreamSec.DSI.ECC.NISTCurves,
  StreamSec.DSI.ArithComp, StreamSec.SSH.Server, StreamSec.SSH.SocketServer,
  StreamSec.SFTP.ServerSubSystem;
procedure ConfigureSftpListener(aServer: TstSSHSocketServer; aKey: TstECDSAKey;
  const aBindIP: string; aPort: Word);
begin
  if aKey = nil then
    raise EArgumentException.Create('Supply a P-256 host key');
  aKey.Enabled := True;
  if not aKey.Enabled then
    raise EArgumentException.Create('The SSH host key could not be loaded');
  aServer.ServerECDSAKey := aKey;
  aServer.BindIP := aBindIP;
  aServer.BindPort := aPort;
  aServer.ServerKeyOptions.SelectedOptionNames := 'ecdsa-sha2-nistp256';
  aServer.KexOptions.SelectedOptionNames := 'ecdh-sha2-nistp256';
  aServer.CipherC2SOptions.SelectedOptionNames := 'aes256-ctr';
  aServer.CipherS2COptions.SelectedOptionNames := 'aes256-ctr';
  aServer.MacC2SOptions.SelectedOptionNames := 'hmac-sha2-256';
  aServer.MacS2COptions.SelectedOptionNames := 'hmac-sha2-256';
  aServer.CompressionC2SOptions.SelectedOptionNames := 'none';
  aServer.CompressionS2COptions.SelectedOptionNames := 'none';
  aServer.UserAuthOptions.SelectedOptionNames := 'password';
end;

Supply the server's existing P-256 private key. Give the corresponding public key to clients through a trusted channel, as described in Configure and verify an SSH server's host key. For an application on the same computer, pass 127.0.0.1 and an unused port, for example 2222. The client uses that address and port to connect.

Choose the directory and grant access

Create a dedicated directory and give the server process the filesystem permissions it needs there. Pass False for aReadOnly to allow uploads. Pass True for a download-only service.

procedure ShareSftpDirectory(aServer: TstSSHSocketServer;
  aFiles: TstSFTPServerSubSystem; const aDirectory: string; aReadOnly: Boolean);
begin
  if (aDirectory = '') or not TDirectory.Exists(aDirectory) then
    raise EArgumentException.Create('Choose an existing SFTP root directory');
  aFiles.RootDir := TPath.GetFullPath(aDirectory);
  aFiles.ReadOnly := aReadOnly;
  aFiles.RequireUserAuthorization := True;
  aFiles.Server := aServer;
end;

Register the subsystem before adding the account. Its registered name, sftp, then appears in the account's subsystem options. Supply the user name and password from your application's account provisioning.

procedure GrantSftpAccount(aServer: TstSSHSocketServer; aUsers: TstSSHServerUsers;
  const aUser, aPassword: string);
begin
  aUsers.Server := aServer;
  with aUsers.Users.Add do begin
    Username := aUser;
    Password := aPassword;
    SubSystems.SelectedOptionNames := 'sftp';
  end;
end;

With this configuration the directory is shared by every account granted sftp. To give each account its own subdirectory, see PerUserSubDir in the SFTP notes.

Start the server and transfer a file

Set the SSH server's Active property to True. From a client that pins the server's public key, sign in and upload a file as /report.txt. It appears as report.txt in the selected directory. Download it again to verify the contents. Upload and download files over SFTP shows the client calls.

Close the client session and set the server's Active property to False before freeing the components.

Reference

TstSSHSocketServer · TstSFTPServerSubSystem · TstSSHServerUsers · TstECDSAKey · StreamSec.SSH.SocketServer · StreamSec.SSH.Server · StreamSec.SFTP.ServerSubSystem · StreamSec.DSI.ArithComp

Related tasks