╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_ssh_protocol.zig ───────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1988 SSH_MSG_USERAUTH_REQUEST => {}, 1989 else => try sendUnimplemented(conn), 1990 } 1991 } 1992 } 1993 1994 fn handleGlobalRequest(conn: *Conn, packet: []const u8) !void { 1995 // byte SSH_MSG_GLOBAL_REQUEST 1996 // string request_name 1997 // boolean want_reply 1998 // …request-specific data we ignore 1999 var r = std.Io.Reader.fixed(packet[1..]); 2000 _ = try takeString(&r, 256); 2001 const want_reply = (try r.takeByte()) != 0; 2002 if (want_reply) { 2003 try conn.writePacket(&.{&[_]u8{SSH_MSG_REQUEST_FAILURE}}); 2004 } 2005 } 2006 2007 /// parse a CHANNEL_OPEN. on a session channel, send CHANNEL_OPEN_CONFIRMATION 2008 /// and return the new Channel. on other types, send CHANNEL_OPEN_FAILURE and 2009 /// return null. 2010 fn handleChannelOpen(conn: *Conn, packet: []const u8, has_channel: bool) !?Channel { 2011 // byte SSH_MSG_CHANNEL_OPEN 2012 // string channel_type 2013 // uint32 sender_channel (peer's id) 2014 // uint32 initial_window 2015 // uint32 max_packet 2016 var r = std.Io.Reader.fixed(packet[1..]); 2017 const ch_type = try takeString(&r, 64); 2018 const remote_id = try r.takeInt(u32, .big); 2019 const initial_window = try r.takeInt(u32, .big); 2020 const max_packet = try r.takeInt(u32, .big); 2021 2022 if (has_channel) { 2023 try sendChannelOpenFailure(conn, remote_id, SSH_OPEN_RESOURCE_SHORTAGE, "only one channel per connection"); 2024 return null; 2025 } 2026 2027 if (!std.mem.eql(u8, ch_type, "session")) { 2028 try sendChannelOpenFailure(conn, remote_id, SSH_OPEN_UNKNOWN_CHANNEL_TYPE, "only session channels are supported"); 2029 return null; 2030 } 2031 2032 // a zero max_packet would leave writeBytes looping without progress 2033 if (max_packet == 0) { 2034 try sendChannelOpenFailure(conn, remote_id, SSH_OPEN_RESOURCE_SHORTAGE, "max packet size must be non-zero"); 2035 return null; 2036 } 2037 2038 const local_id: u32 = 0; // single-channel connection — id is always 0 from our side 2039 var reply: [17]u8 = undefined; 2040 reply[0] = SSH_MSG_CHANNEL_OPEN_CONFIRMATION; 2041 std.mem.writeInt(u32, reply[1..5], remote_id, .big); 2042 std.mem.writeInt(u32, reply[5..9], local_id, .big); 2043 std.mem.writeInt(u32, reply[9..13], initial_recv_window, .big); 2044 std.mem.writeInt(u32, reply[13..17], max_packet_size, .big); 2045 try conn.writePacket(&.{&reply}); 2046 2047 return .{ 2048 .local_id = local_id, 2049 .remote_id = remote_id, 2050 .local_window = initial_recv_window, 2051 .remote_window = initial_window, 2052 .max_packet = max_packet, 2053 }; 2054 } 2055 2056 fn sendChannelOpenFailure(conn: *Conn, remote_id: u32, reason: u32, description: []const u8) !void { 2057 const allocator = conn.allocator; 2058 2059 var reply: std.ArrayList(u8) = .empty; 2060 defer reply.deinit(allocator); 2061 try reply.append(allocator, SSH_MSG_CHANNEL_OPEN_FAILURE); 2062 try writeU32(&reply, allocator, remote_id); 2063 try writeU32(&reply, allocator, reason); 2064 try writeStringField(&reply, allocator, description); 2065 try writeStringField(&reply, allocator, ""); // language tag 2066 try conn.writePacket(&.{reply.items}); 2067 } 2068 2069 /// returns the session request if this one triggered it, else null. an exec 2070 /// command borrows from `packet`. 2071 fn handleChannelRequest(conn: *Conn, ch: *Channel, packet: []const u8) !?Request { 2072 // byte SSH_MSG_CHANNEL_REQUEST 2073 // uint32 recipient_channel (== our local_id) 2074 // string request_type 2075 // boolean want_reply 2076 // …request-specific data 2077 var r = std.Io.Reader.fixed(packet[1..]); 2078 const recipient_channel = try r.takeInt(u32, .big); 2079 if (recipient_channel != ch.local_id) return error.UnknownChannel; 2080 const req_type = try takeString(&r, 64); 2081 const want_reply = (try r.takeByte()) != 0; 2082 2083 if (std.mem.eql(u8, req_type, "pty-req")) { 2084 // string TERM, uint32 width chars, uint32 height rows, uint32 width px, 2085 // uint32 height px, string modes 2086 _ = try takeString(&r, 64); 2087 ch.pty = try takePtySize(&r); 2088 try replyChannelRequest(conn, ch, want_reply, true); 2089 return null; 2090 } 2091 2092 if (std.mem.eql(u8, req_type, "env")) { 2093 // string name, string value. the rest are ignored silently. 2094 const name = try takeString(&r, max_packet_len); 2095 const value = try takeString(&r, max_packet_len); 2096 if (std.mem.eql(u8, name, "GIT_PROTOCOL") and value.len <= ch.git_protocol_buf.len) { 2097 @memcpy(ch.git_protocol_buf[0..value.len], value); 2098 ch.git_protocol_len = value.len; 2099 } 2100 try replyChannelRequest(conn, ch, want_reply, true); 2101 return null; 2102 } 2103 2104 if (std.mem.eql(u8, req_type, "window-change")) { 2105 // uint32 width cells, uint32 height rows, uint32 width px, uint32 height px 2106 const sz = try takePtySize(&r); 2107 if (ch.pty != null) ch.pty = sz; 2108 // spec: window-change MUST NOT request a reply, but accept either 2109 try replyChannelRequest(conn, ch, want_reply, true); 2110 return null; 2111 } 2112 2113 if (std.mem.eql(u8, req_type, "shell")) { 2114 try replyChannelRequest(conn, ch, want_reply, true); 2115 return .{ .shell = ch.pty }; 2116 } 2117 2118 if (std.mem.eql(u8, req_type, "exec")) { 2119 // capture the command string for the handler to inspect 2120 const cmd = try takeString(&r, 4096); 2121 try replyChannelRequest(conn, ch, want_reply, true); 2122 return .{ .exec = .{ 2123 .command = cmd, 2124 .git_protocol = if (ch.git_protocol_len) |len| ch.git_protocol_buf[0..len] else null, 2125 } }; 2126 } 2127 2128 // unknown request kind — fail it 2129 try replyChannelRequest(conn, ch, want_reply, false); 2130 return null; 2131 } 2132 2133 fn replyChannelRequest(conn: *Conn, ch: *Channel, want_reply: bool, success: bool) !void { 2134 if (!want_reply) return; 2135 try sendChannelMessage(conn, ch, if (success) SSH_MSG_CHANNEL_SUCCESS else SSH_MSG_CHANNEL_FAILURE); 2136 } 2137 2138 fn sendChannelMessage(conn: *Conn, ch: *Channel, msg_type: u8) !void { 2139 var packet: [5]u8 = undefined; 2140 packet[0] = msg_type; 2141 std.mem.writeInt(u32, packet[1..5], ch.remote_id, .big); 2142 try conn.writePacket(&.{&packet}); 2143 } 2144 2145 fn handleWindowAdjust(ch: *Channel, packet: []const u8) !void { 2146 // byte SSH_MSG_CHANNEL_WINDOW_ADJUST 2147 // uint32 recipient_channel 2148 // uint32 bytes_to_add 2149 var r = std.Io.Reader.fixed(packet[1..]); 2150 const recipient_channel = try r.takeInt(u32, .big); 2151 if (recipient_channel != ch.local_id) return error.UnknownChannel; 2152 const add = try r.takeInt(u32, .big); 2153 ch.remote_window +|= add; 2154 } 2155 2156 // charge a payload's bytes against the receive window without keeping them. 2157 // the caller credits the window back, choosing the budget that suits it. 2158 fn discardChannelData(ch: *Channel, packet: []const u8, extended: bool) !void { 2159 // byte SSH_MSG_CHANNEL_DATA/EXTENDED_DATA 2160 // uint32 recipient_channel 2161 // [if extended: uint32 type_code] 2162 // string data 2163 const payload = try parseChannelData(packet, extended, ch.local_id); 2164 const data_len: u32 = @intCast(payload.len); 2165 2166 if (data_len > ch.local_window) return error.WindowExceeded; 2167 ch.local_window -= data_len; 2168 } 2169 2170 pub fn parseChannelData(packet: []const u8, extended: bool, expected_channel: u32) ![]const u8 { 2171 if (packet.len < 1) return error.EmptyPacket; 2172 var r = std.Io.Reader.fixed(packet[1..]); 2173 const recipient_channel = try r.takeInt(u32, .big); 2174 if (recipient_channel != expected_channel) return error.UnknownChannel; 2175 if (extended) { 2176 const data_type = try r.takeInt(u32, .big); 2177 if (data_type != SSH_EXTENDED_DATA_STDERR) return error.UnsupportedExtendedData; 2178 } 2179 const data_len = try r.takeInt(u32, .big); 2180 // catches both data_len > remaining (would otherwise be a bounds-check 2181 // panic on the slice) and trailing junk after the declared payload. 2182 if (data_len != r.buffered().len) return error.InvalidChannelData; 2183 return r.buffered(); 2184 } 2185 2186 fn parseChannelId(packet: []const u8, expected_channel: u32) !void { 2187 var r = std.Io.Reader.fixed(packet[1..]); 2188 const recipient_channel = try r.takeInt(u32, .big); 2189 if (recipient_channel != expected_channel) return error.UnknownChannel; 2190 if (r.buffered().len != 0) return error.InvalidChannelMessage; 2191 } 2192 2193 /// send a CHANNEL_WINDOW_ADJUST if our receive window has dipped below half 2194 /// of `budget`, the credit we're willing to have outstanding. used both for 2195 /// the pre-session discard path and by SessionCtx after it consumes a 2196 /// CHANNEL_DATA payload. 2197 fn maybeRefillRecvWindow(conn: *Conn, ch: *Channel, budget: u32) !void { 2198 if (ch.local_window >= budget / 2) return; 2199 const add = budget - ch.local_window; 2200 ch.local_window += add; 2201 var packet: [9]u8 = undefined; 2202 packet[0] = SSH_MSG_CHANNEL_WINDOW_ADJUST; 2203 std.mem.writeInt(u32, packet[1..5], ch.remote_id, .big); 2204 std.mem.writeInt(u32, packet[5..9], add, .big); 2205 try conn.writePacket(&.{&packet}); 2206 } 2207 2208 pub fn writeU32(buf: *std.ArrayList(u8), allocator: std.mem.Allocator, value: u32) !void { 2209 var bytes: [4]u8 = undefined; 2210 std.mem.writeInt(u32, &bytes, value, .big); 2211 try buf.appendSlice(allocator, &bytes); 2212 } 2213 2214 test "pty dimensions have bounded axes, preserving normal and zero sizes" { 2215 for ([_]struct { width: u32, height: u32, expected: PtySize }{ 2216 .{ .width = 80, .height = 24, .expected = .{ .width_cells = 80, .height_cells = 24 } }, 2217 .{ .width = 0, .height = 0, .expected = .{ .width_cells = 0, .height_cells = 0 } }, 2218 .{ .width = 0xffffffff, .height = 0xffffffff, .expected = .{ .width_cells = 512, .height_cells = 128 } }, 2219 }) |case| { 2220 var bytes: [8]u8 = undefined; 2221 std.mem.writeInt(u32, bytes[0..4], case.width, .big); 2222 std.mem.writeInt(u32, bytes[4..8], case.height, .big); 2223 var reader = std.Io.Reader.fixed(&bytes); 2224 try std.testing.expectEqual(case.expected, try takePtySize(&reader)); 2225 } 2226 } 2227 2228 test "channel close before shell or exec is acknowledged" { 2229 const io = std.testing.io; 2230 const allocator = std.testing.allocator; 2231 const key = [_]u8{0x5a} ** 64; 2232 var encoded: [256]u8 = undefined; 2233 var encoded_writer = std.Io.Writer.fixed(&encoded); 2234 var sender = Cipher.init(&key, 0); 2235 var open: std.ArrayList(u8) = .empty; 2236 defer open.deinit(allocator); 2237 try open.append(allocator, SSH_MSG_CHANNEL_OPEN); 2238 try writeStringField(&open, allocator, "session"); 2239 try writeU32(&open, allocator, 7); 2240 try writeU32(&open, allocator, 32768); 2241 try writeU32(&open, allocator, 32768); 2242 try sender.writePacket(io, &encoded_writer, &.{open.items}); 2243 try sender.writePacket(io, &encoded_writer, &.{&.{ SSH_MSG_CHANNEL_CLOSE, 0, 0, 0, 0 }}); 2244 var reader = std.Io.Reader.fixed(encoded_writer.buffered()); 2245 var output: [256]u8 = undefined; 2246 var writer = std.Io.Writer.fixed(&output); 2247 var idle = IdleState{}; 2248 var conn = Conn{ 2249 .io = io, 2250 .allocator = allocator, 2251 .reader = &reader, 2252 .writer = &writer, 2253 .cs_cipher = Cipher.init(&key, 0), 2254 .sc_cipher = Cipher.init(&key, 0), 2255 .rekey = undefined, 2256 .idle = &idle, 2257 }; 2258 const Handler = struct { 2259 pub fn handleSession(_: *@This(), _: *SessionCtx, _: Request) !void { 2260 return error.UnexpectedSession; 2261 } 2262 }; 2263 var handler = Handler{}; 2264 try runChannelLayer(&conn, &([_]u8{0} ** fingerprint_len), &handler); 2265 var output_reader = std.Io.Reader.fixed(writer.buffered()); 2266 var receiver = Cipher.init(&key, 0); 2267 const confirmation = try receiver.readPacket(allocator, &output_reader); 2268 defer allocator.free(confirmation); 2269 try std.testing.expectEqual(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, confirmation[0]); 2270 const close = try receiver.readPacket(allocator, &output_reader); 2271 defer allocator.free(close); 2272 try std.testing.expectEqualSlices(u8, &.{ SSH_MSG_CHANNEL_CLOSE, 0, 0, 0, 7 }, close); 2273 try std.testing.expectEqual(0, output_reader.buffered().len); 2274 } 2275