╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_ssh_protocol.zig ───────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1922 /// returns the session request if this one triggered it, else null. an exec 1923 /// command is owned by `pending_exec`, which outlives the packet it arrived in. 1924 fn handleChannelRequest(conn: *Conn, ch: *Channel, packet: []const u8, pending_exec: *?[]u8) !?Request { 1925 const allocator = conn.allocator; 1926 1927 // byte SSH_MSG_CHANNEL_REQUEST 1928 // uint32 recipient_channel (== our local_id) 1929 // string request_type 1930 // boolean want_reply 1931 // …request-specific data 1932 var r = std.Io.Reader.fixed(packet[1..]); 1933 const recipient_channel = try r.takeInt(u32, .big); 1934 if (recipient_channel != ch.local_id) return error.UnknownChannel; 1935 const req_type = try takeStringField(allocator, &r, 64); 1936 defer allocator.free(req_type); 1937 const want_reply = (try r.takeByte()) != 0; 1938 1939 if (std.mem.eql(u8, req_type, "pty-req")) { 1940 // string TERM, uint32 width chars, uint32 height rows, uint32 width px, 1941 // uint32 height px, string modes 1942 const term = try takeStringField(allocator, &r, 64); 1943 defer allocator.free(term); 1944 ch.pty = try takePtySize(&r); 1945 try replyChannelRequest(conn, ch, want_reply, true); 1946 return null; 1947 } 1948 1949 if (std.mem.eql(u8, req_type, "env")) { 1950 // ignore environment variables silently (we don't pass them anywhere) 1951 try replyChannelRequest(conn, ch, want_reply, true); 1952 return null; 1953 } 1954 1955 if (std.mem.eql(u8, req_type, "window-change")) { 1956 // uint32 width cells, uint32 height rows, uint32 width px, uint32 height px 1957 const sz = try takePtySize(&r); 1958 if (ch.pty != null) ch.pty = sz; 1959 // spec: window-change MUST NOT request a reply, but accept either 1960 try replyChannelRequest(conn, ch, want_reply, true); 1961 return null; 1962 } 1963 1964 if (std.mem.eql(u8, req_type, "shell")) { 1965 try replyChannelRequest(conn, ch, want_reply, true); 1966 return .{ .shell = ch.pty }; 1967 } 1968 1969 if (std.mem.eql(u8, req_type, "exec")) { 1970 // capture the command string for the handler to inspect 1971 const cmd = try takeStringField(allocator, &r, 4096); 1972 if (pending_exec.*) |old| allocator.free(old); 1973 pending_exec.* = cmd; 1974 try replyChannelRequest(conn, ch, want_reply, true); 1975 return .{ .exec = cmd }; 1976 } 1977 1978 // unknown request kind — fail it 1979 try replyChannelRequest(conn, ch, want_reply, false); 1980 return null; 1981 } 1982 1983 fn replyChannelRequest(conn: *Conn, ch: *Channel, want_reply: bool, success: bool) !void { 1984 if (!want_reply) return; 1985 try sendChannelMessage(conn, ch, if (success) SSH_MSG_CHANNEL_SUCCESS else SSH_MSG_CHANNEL_FAILURE); 1986 } 1987 1988 fn sendChannelMessage(conn: *Conn, ch: *Channel, msg_type: u8) !void { 1989 var packet: [5]u8 = undefined; 1990 packet[0] = msg_type; 1991 std.mem.writeInt(u32, packet[1..5], ch.remote_id, .big); 1992 try conn.sc_cipher.writePacket(conn.io, conn.writer, &packet); 1993 } 1994 1995 fn handleWindowAdjust(ch: *Channel, packet: []const u8) !void { 1996 // byte SSH_MSG_CHANNEL_WINDOW_ADJUST 1997 // uint32 recipient_channel 1998 // uint32 bytes_to_add 1999 var r = std.Io.Reader.fixed(packet[1..]); 2000 const recipient_channel = try r.takeInt(u32, .big); 2001 if (recipient_channel != ch.local_id) return error.UnknownChannel; 2002 const add = try r.takeInt(u32, .big); 2003 ch.remote_window +|= add; 2004 } 2005 2006 // charge a payload's bytes against the receive window without keeping them. 2007 // the caller credits the window back, choosing the budget that suits it. 2008 fn discardChannelData(ch: *Channel, packet: []const u8, extended: bool) !void { 2009 // byte SSH_MSG_CHANNEL_DATA/EXTENDED_DATA 2010 // uint32 recipient_channel 2011 // [if extended: uint32 type_code] 2012 // string data 2013 const payload = try parseChannelData(packet, extended, ch.local_id); 2014 const data_len: u32 = @intCast(payload.len); 2015 2016 if (data_len > ch.local_window) return error.WindowExceeded; 2017 ch.local_window -= data_len; 2018 } 2019 2020 pub fn parseChannelData(packet: []const u8, extended: bool, expected_channel: u32) ![]const u8 { 2021 if (packet.len < 1) return error.EmptyPacket; 2022 var r = std.Io.Reader.fixed(packet[1..]); 2023 const recipient_channel = try r.takeInt(u32, .big); 2024 if (recipient_channel != expected_channel) return error.UnknownChannel; 2025 if (extended) { 2026 const data_type = try r.takeInt(u32, .big); 2027 if (data_type != SSH_EXTENDED_DATA_STDERR) return error.UnsupportedExtendedData; 2028 } 2029 const data_len = try r.takeInt(u32, .big); 2030 // catches both data_len > remaining (would otherwise be a bounds-check 2031 // panic on the slice) and trailing junk after the declared payload. 2032 if (data_len != r.buffered().len) return error.InvalidChannelData; 2033 return r.buffered(); 2034 } 2035 2036 fn parseChannelId(packet: []const u8, expected_channel: u32) !void { 2037 if (packet.len < 1) return error.EmptyPacket; 2038 var r = std.Io.Reader.fixed(packet[1..]); 2039 const recipient_channel = try r.takeInt(u32, .big); 2040 if (recipient_channel != expected_channel) return error.UnknownChannel; 2041 if (r.buffered().len != 0) return error.InvalidChannelMessage; 2042 } 2043 2044 /// send a CHANNEL_WINDOW_ADJUST if our receive window has dipped below half 2045 /// of `budget`, the credit we're willing to have outstanding. used both for 2046 /// the pre-session discard path and by SessionCtx after it consumes a 2047 /// CHANNEL_DATA payload. 2048 fn maybeRefillRecvWindow(conn: *Conn, ch: *Channel, budget: u32) !void { 2049 if (ch.local_window >= budget / 2) return; 2050 const add = budget - ch.local_window; 2051 ch.local_window += add; 2052 var adj: std.ArrayList(u8) = .empty; 2053 defer adj.deinit(conn.allocator); 2054 try adj.append(conn.allocator, SSH_MSG_CHANNEL_WINDOW_ADJUST); 2055 try writeU32(&adj, conn.allocator, ch.remote_id); 2056 try writeU32(&adj, conn.allocator, add); 2057 try conn.sc_cipher.writePacket(conn.io, conn.writer, adj.items); 2058 } 2059 2060 pub fn writeU32(buf: *std.ArrayList(u8), allocator: std.mem.Allocator, value: u32) !void { 2061 var bytes: [4]u8 = undefined; 2062 std.mem.writeInt(u32, &bytes, value, .big); 2063 try buf.appendSlice(allocator, &bytes); 2064 } 2065 2066 test "pty dimensions have bounded axes and area, preserving normal and zero sizes" { 2067 for ([_]struct { width: u32, height: u32, expected: PtySize }{ 2068 .{ .width = 80, .height = 24, .expected = .{ .width_cells = 80, .height_cells = 24 } }, 2069 .{ .width = 0, .height = 0, .expected = .{ .width_cells = 0, .height_cells = 0 } }, 2070 .{ .width = 0xffffffff, .height = 0xffffffff, .expected = .{ .width_cells = 512, .height_cells = 128 } }, 2071 }) |case| { 2072 var bytes: [8]u8 = undefined; 2073 std.mem.writeInt(u32, bytes[0..4], case.width, .big); 2074 std.mem.writeInt(u32, bytes[4..8], case.height, .big); 2075 var reader = std.Io.Reader.fixed(&bytes); 2076 try std.testing.expectEqual(case.expected, try takePtySize(&reader)); 2077 } 2078 } 2079 2080 test "channel close before shell or exec is acknowledged" { 2081 const io = std.testing.io; 2082 const allocator = std.testing.allocator; 2083 const key = [_]u8{0x5a} ** 64; 2084 var encoded: [256]u8 = undefined; 2085 var encoded_writer = std.Io.Writer.fixed(&encoded); 2086 var sender = Cipher.init(&key, 0); 2087 var open: std.ArrayList(u8) = .empty; 2088 defer open.deinit(allocator); 2089 try open.append(allocator, SSH_MSG_CHANNEL_OPEN); 2090 try writeStringField(&open, allocator, "session"); 2091 try writeU32(&open, allocator, 7); 2092 try writeU32(&open, allocator, 32768); 2093 try writeU32(&open, allocator, 32768); 2094 try sender.writePacket(io, &encoded_writer, open.items); 2095 try sender.writePacket(io, &encoded_writer, &.{ SSH_MSG_CHANNEL_CLOSE, 0, 0, 0, 0 }); 2096 var reader = std.Io.Reader.fixed(encoded_writer.buffered()); 2097 var output: [256]u8 = undefined; 2098 var writer = std.Io.Writer.fixed(&output); 2099 var conn = Conn{ 2100 .io = io, 2101 .allocator = allocator, 2102 .reader = &reader, 2103 .writer = &writer, 2104 .cs_cipher = Cipher.init(&key, 0), 2105 .sc_cipher = Cipher.init(&key, 0), 2106 .rekey = undefined, 2107 }; 2108 const Handler = struct { 2109 pub fn handleSession(_: *@This(), _: *SessionCtx, _: Request) !void { 2110 return error.UnexpectedSession; 2111 } 2112 }; 2113 var handler = Handler{}; 2114 try runChannelLayer(&conn, &([_]u8{0} ** fingerprint_len), &handler); 2115 var output_reader = std.Io.Reader.fixed(writer.buffered()); 2116 var receiver = Cipher.init(&key, 0); 2117 const confirmation = try receiver.readPacket(allocator, &output_reader); 2118 defer allocator.free(confirmation); 2119 try std.testing.expectEqual(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, confirmation[0]); 2120 const close = try receiver.readPacket(allocator, &output_reader); 2121 defer allocator.free(close); 2122 try std.testing.expectEqualSlices(u8, &.{ SSH_MSG_CHANNEL_CLOSE, 0, 0, 0, 7 }, close); 2123 try std.testing.expectEqual(0, output_reader.buffered().len); 2124 } 2125