Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Authenticate an SSH Tunnel with a Private Key
See more POP3 Examples
Demonstrates the Chilkat MailMan.SshAuthenticatePk method, which authenticates with the SSH server using public-key authentication after an SSH tunnel has been opened. The first argument is the SSH account name and the second is the SSH private key (an SshKey object). This example opens a tunnel, loads an OpenSSH private key, authenticates with it, and runs a POP3 operation through the tunnel.
Background: SSH public-key authentication replaces a password with a key pair: the public key is installed on the SSH server, and the client proves it holds the matching private key without ever transmitting a secret. This is the preferred method for automated systems — there is no password to embed or rotate, and the key can be revoked server-side. Chilkat reads the key with an
SshKey object, which understands common formats such as OpenSSH and PuTTY.Chilkat Pascal (Lazarus/Delphi) Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.MailMan,
Chilkat.SshKey;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
mailman: TMailMan;
sshKey: TSshKey;
keyText: string;
count: Integer;
begin
success := False;
// Demonstrates the MailMan.SshAuthenticatePk method, which authenticates with the SSH server
// using public-key authentication after an SSH tunnel has been opened. The 1st argument is
// the SSH account name and the 2nd is the SSH private key.
mailman := TMailMan.Create;
// Configure the POP3 server connection. These connections will travel through the tunnel.
mailman.MailHost := 'pop.example.com';
mailman.MailPort := 995;
mailman.PopSsl := True;
mailman.PopUsername := 'user@example.com';
mailman.PopPassword := 'myPassword';
// Open the SSH tunnel first.
success := mailman.SshOpenTunnel('ssh.example.com',22);
if (success = False) then
begin
WriteLn(mailman.LastErrorText);
Exit;
end;
// Load the SSH private key from an OpenSSH-format key file.
sshKey := TSshKey.Create;
keyText := sshKey.LoadText('qa_data/ssh/id_rsa');
if (sshKey.LastMethodSuccess = False) then
begin
WriteLn(sshKey.LastErrorText);
Exit;
end;
success := sshKey.FromOpenSshPrivateKey(keyText);
if (success = False) then
begin
WriteLn(sshKey.LastErrorText);
Exit;
end;
// Authenticate with the SSH server using the private key.
success := mailman.SshAuthenticatePk('sshUser',sshKey);
if (success = False) then
begin
WriteLn(mailman.LastErrorText);
Exit;
end;
// POP3 operations now travel through the SSH tunnel.
count := mailman.GetMailboxCount();
if (count < 0) then
begin
WriteLn('Failed to get the mailbox count.');
end
else
begin
WriteLn('Mailbox count: ' + count);
end;
success := mailman.SshCloseTunnel();
if (success = False) then
begin
WriteLn(mailman.LastErrorText);
Exit;
end;
// Note: The path "qa_data/ssh/id_rsa" is a relative local filesystem path,
// relative to the current working directory of the running application.
mailman.Free;
sshKey.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.