Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

Authenticate an IMAP SSH Tunnel with a Private Key

See more IMAP Examples

Demonstrates the Chilkat Imap.SshAuthenticatePk method, which authenticates the SSH tunnel using a username and an SshKey private key. The first argument is the SSH login name and the second is the private key. Call SshOpenTunnel first; the matching public key must already be authorized for the login on the SSH server.

Note: The private-key path is relative to the application's current working directory. Supply the path to your own key file.

Background: Public-key authentication is the usual choice for unattended/automated SSH access: the private key stays on the client and the server is configured to trust the corresponding public key, so no reusable password travels over the wire. The SshKey object can import keys in several formats — this example uses an OpenSSH private key.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
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.Imap,
  Chilkat.SshKey;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  imap: TImap;
  sshPort: Integer;
  privKey: TSshKey;
  keyStr: string;

begin
  success := False;

  //  Demonstrates the Imap.SshAuthenticatePk method, which authenticates the SSH tunnel using a
  //  username and a private key.  The 1st argument is the SSH login name and the 2nd is an SshKey
  //  private key.  Call SshOpenTunnel first.

  imap := TImap.Create;

  sshPort := 22;
  success := imap.SshOpenTunnel('ssh.example.com',sshPort);
  if (success = False) then
    begin
      WriteLn(imap.LastErrorText);
      Exit;
    end;

  //  Load the SSH private key.  LoadText returns the file's text, which is then imported.
  privKey := TSshKey.Create;
  keyStr := privKey.LoadText('qa_data/id_rsa');
  if (privKey.LastMethodSuccess = False) then
    begin
      WriteLn(privKey.LastErrorText);
      Exit;
    end;
  success := privKey.FromOpenSshPrivateKey(keyStr);
  if (success = False) then
    begin
      WriteLn(privKey.LastErrorText);
      Exit;
    end;

  //  Authenticate the SSH tunnel with the private key.  The matching public key must already be
  //  authorized for the login on the SSH server.
  success := imap.SshAuthenticatePk('sshUser',privKey);
  if (success = False) then
    begin
      WriteLn(imap.LastErrorText);
      Exit;
    end;

  //  With the SSH tunnel in place, connect and log in to the IMAP server as usual.  The IMAP
  //  traffic flows through the tunnel automatically.
  imap.Ssl := True;
  imap.Port := 993;
  success := imap.Connect('imap.example.com');
  if (success = False) then
    begin
      WriteLn(imap.LastErrorText);
      Exit;
    end;
  success := imap.Login('user@example.com','myPassword');
  if (success = False) then
    begin
      WriteLn(imap.LastErrorText);
      Exit;
    end;

  success := imap.Disconnect();
  if (success = False) then
    begin
      WriteLn(imap.LastErrorText);
      Exit;
    end;

  success := imap.SshCloseTunnel();
  if (success = False) then
    begin
      WriteLn(imap.LastErrorText);
      Exit;
    end;


  imap.Free;
  privKey.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.