summaryrefslogtreecommitdiff
path: root/src/ssh.zig
blob: 549e528b668410d8491cb00b44e29c3a0671591d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const std = @import("std");
const ssh = @cImport(@cInclude("libssh/libssh.h"));

pub const Client = struct {
    session: ?ssh.ssh_session,

    pub fn deinit(self: *Client) void {
        if (self.session) |session| {
            if (ssh.ssh_is_connected(session) == 1) {
                ssh.ssh_disconnect(session);
            }
            ssh.ssh_free(session);
            self.session = null;
        }
    }
    pub fn init(hostname: [*]const u8) !Client {
        var client = Client{
            .session = ssh.ssh_new(),
        };
        var port: i64 = 22;

        if (client.session) |session| {
            _ = ssh.ssh_options_set(session, ssh.SSH_OPTIONS_HOST, hostname);
            _ = ssh.ssh_options_set(session, ssh.SSH_OPTIONS_PORT, &port);
            //var verbosity = ssh.SSH_LOG_PROTOCOL;
            //_ = ssh.ssh_options_set(session, ssh.SSH_OPTIONS_LOG_VERBOSITY, &verbosity);

            var rc = ssh.ssh_connect(session);
            if (rc != ssh.SSH_OK) {
                std.log.info("Error connecting to localhost: {s}\n", .{ssh.ssh_get_error(session)});
                std.os.exit(2);
            }

            //if (ssh.verify_knownhost(session) < 0) {
            //    std.log.info("knownhost verification error", .{});
            //    std.os.exit(3);
            //}

            if (ssh.ssh_userauth_publickey_auto(session, null, null) == ssh.SSH_AUTH_ERROR) {
                std.log.info("Error authenticating with public key: {s}\n", .{ssh.ssh_get_error(session)});
                std.os.exit(3);
            }
        } else {
            std.log.info("failed to initialise ssh session\n", .{});
            std.os.exit(1);
        }
        return client;
    }
    pub fn run(self: *Client, cmd: [*]const u8, writer: anytype) !void {
        if (self.session) |session| {
            if (ssh.ssh_channel_new(session)) |channel| {
                defer ssh.ssh_channel_free(channel);
                defer _ = ssh.ssh_channel_close(channel);
                if (ssh.ssh_channel_open_session(channel) != ssh.SSH_OK) {
                    std.log.info("Error opening channel session: {s}\n", .{ssh.ssh_get_error(session)});
                    std.os.exit(3);
                }

                if (ssh.ssh_channel_request_exec(channel, cmd) != ssh.SSH_OK) {
                    std.log.info("Error executing command: {s}\n", .{ssh.ssh_get_error(session)});
                    std.os.exit(4);
                }

                var buffer: [256]u8 = undefined;
                var nbytes: usize = @intCast(ssh.ssh_channel_read(channel, &buffer, buffer.len, 0));
                while (nbytes > 0) : (nbytes = @intCast(ssh.ssh_channel_read(channel, &buffer, buffer.len, 0))) {
                    var w = try writer.write(buffer[0..nbytes]);
                    if (w != nbytes) {
                        std.os.exit(5);
                    }
                }
                _ = ssh.ssh_channel_send_eof(channel);
            } else {
                std.log.info("Error creating channel", .{});
            }
        }
    }
};