Sample code for 30+ languages & platforms
Zig

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 Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    // 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.

    const imap = try chilkat.Imap.init();
    defer imap.deinit();

    const ssh_port = 22;
    imap.sshOpenTunnel("ssh.example.com", ssh_port) catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    // Load the SSH private key.  LoadText returns the file's text, which is then imported.
    const priv_key = try chilkat.SshKey.init();
    defer priv_key.deinit();
    const key_str = priv_key.loadText(alloc, "qa_data/id_rsa") catch {
        std.debug.print("{s}\n", .{try priv_key.getLastErrorText(alloc)});
        return;
    };

    priv_key.fromOpenSshPrivateKey(key_str) catch {
        std.debug.print("{s}\n", .{try priv_key.getLastErrorText(alloc)});
        return;
    };

    // Authenticate the SSH tunnel with the private key.  The matching public key must already be
    // authorized for the login on the SSH server.
    imap.sshAuthenticatePk("sshUser", priv_key) catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    // 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.setSsl(true);
    imap.setPort(993);
    imap.connect("imap.example.com") catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    imap.login("user@example.com", "myPassword") catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    imap.disconnect() catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    imap.sshCloseTunnel() catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };
}