╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_ssh_protocol.zig ───────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2076 } 2077 2078 /// returns the session request if this one triggered it, else null. an exec 2079 /// command borrows from `packet`. 2080 fn handleChannelRequest(conn: *Conn, ch: *Channel, packet: []const u8) !?Request { 2081 // byte SSH_MSG_CHANNEL_REQUEST 2082 // uint32 recipient_channel (== our local_id) 2083 // string request_type 2084 // boolean want_reply 2085 // …request-specific data 2086 var r = std.Io.Reader.fixed(packet[1..]); 2087 const recipient_channel = try r.takeInt(u32, .big); 2088 if (recipient_channel != ch.local_id) return error.UnknownChannel; 2089 const req_type = try takeString(&r, 64); 2090 const want_reply = (try r.takeByte()) != 0; 2091 2092 if (std.mem.eql(u8, req_type, "pty-req")) { 2093 // string TERM, uint32 width chars, uint32 height rows, uint32 width px, 2094 // uint32 height px, string modes 2095 _ = try takeString(&r, 64); 2096 ch.pty = try takePtySize(&r); 2097 try replyChannelRequest(conn, ch, want_reply, true); 2098 return null; 2099 } 2100 2101 if (std.mem.eql(u8, req_type, "env")) { 2102 // string name, string value. the rest are ignored silently. 2103 const name = try takeString(&r, max_packet_len); 2104 const value = try takeString(&r, max_packet_len); 2105 if (std.mem.eql(u8, name, "GIT_PROTOCOL") and value.len <= ch.git_protocol_buf.len) { 2106 @memcpy(ch.git_protocol_buf[0..value.len], value); 2107 ch.git_protocol_len = value.len; 2108 } else if (std.mem.eql(u8, name, "NO_COLOR")) { 2109 ch.color = value.len == 0; 2110 } 2111 try replyChannelRequest(conn, ch, want_reply, true); 2112 return null; 2113 } 2114 2115 if (std.mem.eql(u8, req_type, "window-change")) { 2116 // uint32 width cells, uint32 height rows, uint32 width px, uint32 height px 2117 const sz = try takePtySize(&r); 2118 if (ch.pty != null) ch.pty = sz; 2119 // spec: window-change MUST NOT request a reply, but accept either 2120 try replyChannelRequest(conn, ch, want_reply, true); 2121 return null; 2122 } 2123 2124 if (std.mem.eql(u8, req_type, "shell")) { 2125 try replyChannelRequest(conn, ch, want_reply, true); 2126 return .{ .shell = .{ .pty = ch.pty, .color = ch.color } }; 2127 } 2128 2129 if (std.mem.eql(u8, req_type, "exec")) { 2130 // capture the command string for the handler to inspect 2131 const cmd = try takeString(&r, 4096); 2132 try replyChannelRequest(conn, ch, want_reply, true); 2133 return .{ .exec = .{ 2134 .command = cmd, 2135 .git_protocol = if (ch.git_protocol_len) |len| ch.git_protocol_buf[0..len] else null, 2136 } }; 2137 } 2138 2139 // unknown request kind — fail it 2140 try replyChannelRequest(conn, ch, want_reply, false); 2141 return null; 2142 } 2143 2144 fn replyChannelRequest(conn: *Conn, ch: *Channel, want_reply: bool, success: bool) !void { 2145 if (!want_reply) return; 2146 try sendChannelMessage(conn, ch, if (success) SSH_MSG_CHANNEL_SUCCESS else SSH_MSG_CHANNEL_FAILURE); 2147 } 2148 2149 fn sendChannelMessage(conn: *Conn, ch: *Channel, msg_type: u8) !void { 2150 var packet: [5]u8 = undefined; 2151 packet[0] = msg_type; 2152 std.mem.writeInt(u32, packet[1..5], ch.remote_id, .big); 2153 try conn.writePacket(&.{&packet}); 2154 } 2155 2156 fn handleWindowAdjust(ch: *Channel, packet: []const u8) !void { 2157 // byte SSH_MSG_CHANNEL_WINDOW_ADJUST 2158 // uint32 recipient_channel 2159 // uint32 bytes_to_add 2160 var r = std.Io.Reader.fixed(packet[1..]); 2161 const recipient_channel = try r.takeInt(u32, .big); 2162 if (recipient_channel != ch.local_id) return error.UnknownChannel; 2163 const add = try r.takeInt(u32, .big); 2164 ch.remote_window +|= add; 2165 } 2166 2167 // charge a payload's bytes against the receive window without keeping them. 2168 // the caller credits the window back, choosing the budget that suits it. 2169 fn discardChannelData(ch: *Channel, packet: []const u8, extended: bool) !void { 2170 // byte SSH_MSG_CHANNEL_DATA/EXTENDED_DATA 2171 // uint32 recipient_channel 2172 // [if extended: uint32 type_code] 2173 // string data 2174 const payload = try parseChannelData(packet, extended, ch.local_id); 2175 const data_len: u32 = @intCast(payload.len); 2176 2177 if (data_len > ch.local_window) return error.WindowExceeded; 2178 ch.local_window -= data_len; 2179 } 2180 2181 pub fn parseChannelData(packet: []const u8, extended: bool, expected_channel: u32) ![]const u8 { 2182 if (packet.len < 1) return error.EmptyPacket; 2183 var r = std.Io.Reader.fixed(packet[1..]); 2184 const recipient_channel = try r.takeInt(u32, .big); 2185 if (recipient_channel != expected_channel) return error.UnknownChannel; 2186 if (extended) { 2187 const data_type = try r.takeInt(u32, .big); 2188 if (data_type != SSH_EXTENDED_DATA_STDERR) return error.UnsupportedExtendedData; 2189 } 2190 const data_len = try r.takeInt(u32, .big); 2191 // catches both data_len > remaining (would otherwise be a bounds-check 2192 // panic on the slice) and trailing junk after the declared payload. 2193 if (data_len != r.buffered().len) return error.InvalidChannelData; 2194 return r.buffered(); 2195 } 2196 2197 fn parseChannelId(packet: []const u8, expected_channel: u32) !void { 2198 var r = std.Io.Reader.fixed(packet[1..]); 2199 const recipient_channel = try r.takeInt(u32, .big); 2200 if (recipient_channel != expected_channel) return error.UnknownChannel; 2201 if (r.buffered().len != 0) return error.InvalidChannelMessage; 2202 } 2203 2204 /// send a CHANNEL_WINDOW_ADJUST if our receive window has dipped below half 2205 /// of `budget`, the credit we're willing to have outstanding. used both for 2206 /// the pre-session discard path and by SessionCtx after it consumes a 2207 /// CHANNEL_DATA payload. 2208 fn maybeRefillRecvWindow(conn: *Conn, ch: *Channel, budget: u32) !void { 2209 if (ch.local_window >= budget / 2) return; 2210 const add = budget - ch.local_window; 2211 ch.local_window += add; 2212 var packet: [9]u8 = undefined; 2213 packet[0] = SSH_MSG_CHANNEL_WINDOW_ADJUST; 2214 std.mem.writeInt(u32, packet[1..5], ch.remote_id, .big); 2215 std.mem.writeInt(u32, packet[5..9], add, .big); 2216 try conn.writePacket(&.{&packet}); 2217 } 2218 2219 pub fn writeU32(buf: *std.ArrayList(u8), allocator: std.mem.Allocator, value: u32) !void { 2220 var bytes: [4]u8 = undefined; 2221 std.mem.writeInt(u32, &bytes, value, .big); 2222 try buf.appendSlice(allocator, &bytes); 2223 } 2224 2225 test "pty dimensions have bounded axes, preserving normal and zero sizes" { 2226 for ([_]struct { width: u32, height: u32, expected: PtySize }{ 2227 .{ .width = 80, .height = 24, .expected = .{ .width_cells = 80, .height_cells = 24 } }, 2228 .{ .width = 0, .height = 0, .expected = .{ .width_cells = 0, .height_cells = 0 } }, 2229 .{ .width = 0xffffffff, .height = 0xffffffff, .expected = .{ .width_cells = 512, .height_cells = 128 } }, 2230 }) |case| { 2231 var bytes: [8]u8 = undefined; 2232 std.mem.writeInt(u32, bytes[0..4], case.width, .big); 2233 std.mem.writeInt(u32, bytes[4..8], case.height, .big); 2234 var reader = std.Io.Reader.fixed(&bytes); 2235 try std.testing.expectEqual(case.expected, try takePtySize(&reader)); 2236 } 2237 } 2238 2239 test "channel close before shell or exec is acknowledged" { 2240 const io = std.testing.io; 2241 const allocator = std.testing.allocator; 2242 const key = [_]u8{0x5a} ** 64; 2243 var encoded: [256]u8 = undefined; 2244 var encoded_writer = std.Io.Writer.fixed(&encoded); 2245 var sender = Cipher.init(&key, 0); 2246 var open: std.ArrayList(u8) = .empty; 2247 defer open.deinit(allocator); 2248 try open.append(allocator, SSH_MSG_CHANNEL_OPEN); 2249 try writeStringField(&open, allocator, "session"); 2250 try writeU32(&open, allocator, 7); 2251 try writeU32(&open, allocator, 32768); 2252 try writeU32(&open, allocator, 32768); 2253 try sender.writePacket(io, &encoded_writer, &.{open.items}); 2254 try sender.writePacket(io, &encoded_writer, &.{&.{ SSH_MSG_CHANNEL_CLOSE, 0, 0, 0, 0 }}); 2255 var reader = std.Io.Reader.fixed(encoded_writer.buffered()); 2256 var output: [256]u8 = undefined; 2257 var writer = std.Io.Writer.fixed(&output); 2258 var idle = IdleState{}; 2259 var conn = Conn{ 2260 .io = io, 2261 .allocator = allocator, 2262 .reader = &reader, 2263 .writer = &writer, 2264 .cs_cipher = Cipher.init(&key, 0), 2265 .sc_cipher = Cipher.init(&key, 0), 2266 .rekey = undefined, 2267 .idle = &idle, 2268 }; 2269 const Handler = struct { 2270 pub fn handleSession(_: *@This(), _: *SessionCtx, _: Request) !void { 2271 return error.UnexpectedSession; 2272 } 2273 }; 2274 var handler = Handler{}; 2275 try runChannelLayer(&conn, &([_]u8{0} ** fingerprint_len), &handler); 2276 var output_reader = std.Io.Reader.fixed(writer.buffered()); 2277 var receiver = Cipher.init(&key, 0); 2278 const confirmation = try receiver.readPacket(allocator, &output_reader); 2279 defer allocator.free(confirmation); 2280 try std.testing.expectEqual(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, confirmation[0]); 2281 const close = try receiver.readPacket(allocator, &output_reader); 2282 defer allocator.free(close); 2283 try std.testing.expectEqualSlices(u8, &.{ SSH_MSG_CHANNEL_CLOSE, 0, 0, 0, 7 }, close); 2284 try std.testing.expectEqual(0, output_reader.buffered().len); 2285 } 2286