╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_ssh_protocol.zig ───────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2145 _ = try takeString(&r, 64); 2146 ch.pty = try takePtySize(&r); 2147 try replyChannelRequest(conn, ch, want_reply, true); 2148 return null; 2149 } 2150 2151 if (std.mem.eql(u8, req_type, "env")) { 2152 // string name, string value. the rest are ignored silently. 2153 const name = try takeString(&r, max_packet_len); 2154 const value = try takeString(&r, max_packet_len); 2155 if (std.mem.eql(u8, name, "GIT_PROTOCOL") and value.len <= ch.git_protocol_buf.len) { 2156 @memcpy(ch.git_protocol_buf[0..value.len], value); 2157 ch.git_protocol_len = value.len; 2158 } else if (std.mem.eql(u8, name, "NO_COLOR")) { 2159 ch.color = value.len == 0; 2160 } 2161 try replyChannelRequest(conn, ch, want_reply, true); 2162 return null; 2163 } 2164 2165 if (std.mem.eql(u8, req_type, "window-change")) { 2166 // uint32 width cells, uint32 height rows, uint32 width px, uint32 height px 2167 const sz = try takePtySize(&r); 2168 if (ch.pty != null) ch.pty = sz; 2169 // spec: window-change MUST NOT request a reply, but accept either 2170 try replyChannelRequest(conn, ch, want_reply, true); 2171 return null; 2172 } 2173 2174 if (std.mem.eql(u8, req_type, "shell")) { 2175 try replyChannelRequest(conn, ch, want_reply, true); 2176 return .{ .shell = .{ .pty = ch.pty, .color = ch.color } }; 2177 } 2178 2179 if (std.mem.eql(u8, req_type, "exec")) { 2180 // capture the command string for the handler to inspect 2181 const cmd = try takeString(&r, 4096); 2182 try replyChannelRequest(conn, ch, want_reply, true); 2183 return .{ .exec = .{ 2184 .command = cmd, 2185 .git_protocol = if (ch.git_protocol_len) |len| ch.git_protocol_buf[0..len] else null, 2186 } }; 2187 } 2188 2189 // unknown request kind — fail it 2190 try replyChannelRequest(conn, ch, want_reply, false); 2191 return null; 2192 } 2193 2194 fn replyChannelRequest(conn: *Conn, ch: *Channel, want_reply: bool, success: bool) !void { 2195 if (!want_reply) return; 2196 try sendChannelMessage(conn, ch, if (success) SSH_MSG_CHANNEL_SUCCESS else SSH_MSG_CHANNEL_FAILURE); 2197 } 2198 2199 fn sendChannelMessage(conn: *Conn, ch: *Channel, msg_type: u8) !void { 2200 var packet: [5]u8 = undefined; 2201 packet[0] = msg_type; 2202 std.mem.writeInt(u32, packet[1..5], ch.remote_id, .big); 2203 try conn.writePacket(&.{&packet}); 2204 } 2205 2206 fn handleWindowAdjust(ch: *Channel, packet: []const u8) !void { 2207 // byte SSH_MSG_CHANNEL_WINDOW_ADJUST 2208 // uint32 recipient_channel 2209 // uint32 bytes_to_add 2210 var r = std.Io.Reader.fixed(packet[1..]); 2211 const recipient_channel = try r.takeInt(u32, .big); 2212 if (recipient_channel != ch.local_id) return error.UnknownChannel; 2213 const add = try r.takeInt(u32, .big); 2214 ch.remote_window +|= add; 2215 } 2216 2217 // charge a payload's bytes against the receive window without keeping them. 2218 // the caller credits the window back, choosing the budget that suits it. 2219 fn discardChannelData(ch: *Channel, packet: []const u8, extended: bool) !void { 2220 // byte SSH_MSG_CHANNEL_DATA/EXTENDED_DATA 2221 // uint32 recipient_channel 2222 // [if extended: uint32 type_code] 2223 // string data 2224 const payload = try parseChannelData(packet, extended, ch.local_id); 2225 const data_len: u32 = @intCast(payload.len); 2226 2227 if (data_len > ch.local_window) return error.WindowExceeded; 2228 ch.local_window -= data_len; 2229 } 2230 2231 pub fn parseChannelData(packet: []const u8, extended: bool, expected_channel: u32) ![]const u8 { 2232 if (packet.len < 1) return error.EmptyPacket; 2233 var r = std.Io.Reader.fixed(packet[1..]); 2234 const recipient_channel = try r.takeInt(u32, .big); 2235 if (recipient_channel != expected_channel) return error.UnknownChannel; 2236 if (extended) { 2237 const data_type = try r.takeInt(u32, .big); 2238 if (data_type != SSH_EXTENDED_DATA_STDERR) return error.UnsupportedExtendedData; 2239 } 2240 const data_len = try r.takeInt(u32, .big); 2241 // catches both data_len > remaining (would otherwise be a bounds-check 2242 // panic on the slice) and trailing junk after the declared payload. 2243 if (data_len != r.buffered().len) return error.InvalidChannelData; 2244 return r.buffered(); 2245 } 2246 2247 fn parseChannelId(packet: []const u8, expected_channel: u32) !void { 2248 var r = std.Io.Reader.fixed(packet[1..]); 2249 const recipient_channel = try r.takeInt(u32, .big); 2250 if (recipient_channel != expected_channel) return error.UnknownChannel; 2251 if (r.buffered().len != 0) return error.InvalidChannelMessage; 2252 } 2253 2254 /// send a CHANNEL_WINDOW_ADJUST if our receive window has dipped below half 2255 /// of `budget`, the credit we're willing to have outstanding. used both for 2256 /// the pre-session discard path and by SessionCtx after it consumes a 2257 /// CHANNEL_DATA payload. 2258 fn maybeRefillRecvWindow(conn: *Conn, ch: *Channel, budget: u32) !void { 2259 if (ch.local_window >= budget / 2) return; 2260 const add = budget - ch.local_window; 2261 ch.local_window += add; 2262 var packet: [9]u8 = undefined; 2263 packet[0] = SSH_MSG_CHANNEL_WINDOW_ADJUST; 2264 std.mem.writeInt(u32, packet[1..5], ch.remote_id, .big); 2265 std.mem.writeInt(u32, packet[5..9], add, .big); 2266 try conn.writePacket(&.{&packet}); 2267 } 2268 2269 pub fn writeU32(buf: *std.ArrayList(u8), allocator: std.mem.Allocator, value: u32) !void { 2270 var bytes: [4]u8 = undefined; 2271 std.mem.writeInt(u32, &bytes, value, .big); 2272 try buf.appendSlice(allocator, &bytes); 2273 } 2274 2275 test "pty dimensions have bounded axes, preserving normal and zero sizes" { 2276 for ([_]struct { width: u32, height: u32, expected: PtySize }{ 2277 .{ .width = 80, .height = 24, .expected = .{ .width_cells = 80, .height_cells = 24 } }, 2278 .{ .width = 0, .height = 0, .expected = .{ .width_cells = 0, .height_cells = 0 } }, 2279 .{ .width = 0xffffffff, .height = 0xffffffff, .expected = .{ .width_cells = 512, .height_cells = 128 } }, 2280 }) |case| { 2281 var bytes: [8]u8 = undefined; 2282 std.mem.writeInt(u32, bytes[0..4], case.width, .big); 2283 std.mem.writeInt(u32, bytes[4..8], case.height, .big); 2284 var reader = std.Io.Reader.fixed(&bytes); 2285 try std.testing.expectEqual(case.expected, try takePtySize(&reader)); 2286 } 2287 } 2288 2289 test "channel close before shell or exec is acknowledged" { 2290 const io = std.testing.io; 2291 const allocator = std.testing.allocator; 2292 const key: [64]u8 = @splat(0x5a); 2293 var encoded: [256]u8 = undefined; 2294 var encoded_writer = std.Io.Writer.fixed(&encoded); 2295 var sender = Cipher.init(&key, 0); 2296 var open: std.ArrayList(u8) = .empty; 2297 defer open.deinit(allocator); 2298 try open.append(allocator, SSH_MSG_CHANNEL_OPEN); 2299 try writeStringField(&open, allocator, "session"); 2300 try writeU32(&open, allocator, 7); 2301 try writeU32(&open, allocator, 32768); 2302 try writeU32(&open, allocator, 32768); 2303 try sender.writePacket(io, &encoded_writer, &.{open.items}); 2304 try sender.writePacket(io, &encoded_writer, &.{&.{ SSH_MSG_CHANNEL_CLOSE, 0, 0, 0, 0 }}); 2305 var reader = std.Io.Reader.fixed(encoded_writer.buffered()); 2306 var output: [256]u8 = undefined; 2307 var writer = std.Io.Writer.fixed(&output); 2308 var idle = IdleState{}; 2309 var conn = Conn{ 2310 .io = io, 2311 .allocator = allocator, 2312 .reader = &reader, 2313 .writer = &writer, 2314 .cs_cipher = Cipher.init(&key, 0), 2315 .sc_cipher = Cipher.init(&key, 0), 2316 .rekey = undefined, 2317 .idle = &idle, 2318 }; 2319 const Handler = struct { 2320 pub fn handleSession(_: *@This(), _: *SessionCtx, _: Request) !void { 2321 return error.UnexpectedSession; 2322 } 2323 }; 2324 var handler = Handler{}; 2325 try runChannelLayer(&conn, &.{ .fingerprint = @splat(0), .blob = "" }, &handler); 2326 var output_reader = std.Io.Reader.fixed(writer.buffered()); 2327 var receiver = Cipher.init(&key, 0); 2328 const confirmation = try receiver.readPacket(allocator, &output_reader); 2329 defer allocator.free(confirmation); 2330 try std.testing.expectEqual(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, confirmation[0]); 2331 const close = try receiver.readPacket(allocator, &output_reader); 2332 defer allocator.free(close); 2333 try std.testing.expectEqualSlices(u8, &.{ SSH_MSG_CHANNEL_CLOSE, 0, 0, 0, 7 }, close); 2334 try std.testing.expectEqual(0, output_reader.buffered().len); 2335 } 2336