╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_ssh_protocol.zig ───────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1893 try replyChannelRequest(conn, ch, want_reply, true); 1894 return null; 1895 } 1896 1897 if (std.mem.eql(u8, req_type, "env")) { 1898 // ignore environment variables silently (we don't pass them anywhere) 1899 try replyChannelRequest(conn, ch, want_reply, true); 1900 return null; 1901 } 1902 1903 if (std.mem.eql(u8, req_type, "window-change")) { 1904 // uint32 width cells, uint32 height rows, uint32 width px, uint32 height px 1905 const sz = try takePtySize(&r); 1906 if (ch.pty != null) ch.pty = sz; 1907 // spec: window-change MUST NOT request a reply, but accept either 1908 try replyChannelRequest(conn, ch, want_reply, true); 1909 return null; 1910 } 1911 1912 if (std.mem.eql(u8, req_type, "shell")) { 1913 try replyChannelRequest(conn, ch, want_reply, true); 1914 return .{ .shell = ch.pty }; 1915 } 1916 1917 if (std.mem.eql(u8, req_type, "exec")) { 1918 // capture the command string for the handler to inspect 1919 const cmd = try takeStringField(allocator, &r, 4096); 1920 if (pending_exec.*) |old| allocator.free(old); 1921 pending_exec.* = cmd; 1922 try replyChannelRequest(conn, ch, want_reply, true); 1923 return .{ .exec = cmd }; 1924 } 1925 1926 // unknown request kind — fail it 1927 try replyChannelRequest(conn, ch, want_reply, false); 1928 return null; 1929 } 1930 1931 fn replyChannelRequest(conn: *Conn, ch: *Channel, want_reply: bool, success: bool) !void { 1932 if (!want_reply) return; 1933 var reply: std.ArrayList(u8) = .empty; 1934 defer reply.deinit(conn.allocator); 1935 try reply.append(conn.allocator, if (success) SSH_MSG_CHANNEL_SUCCESS else SSH_MSG_CHANNEL_FAILURE); 1936 try writeU32(&reply, conn.allocator, ch.remote_id); 1937 try conn.sc_cipher.writePacket(conn.io, conn.writer, reply.items); 1938 } 1939 1940 fn handleWindowAdjust(ch: *Channel, packet: []const u8) !void { 1941 // byte SSH_MSG_CHANNEL_WINDOW_ADJUST 1942 // uint32 recipient_channel 1943 // uint32 bytes_to_add 1944 var r = std.Io.Reader.fixed(packet[1..]); 1945 const recipient_channel = try r.takeInt(u32, .big); 1946 if (recipient_channel != ch.local_id) return error.UnknownChannel; 1947 const add = try r.takeInt(u32, .big); 1948 ch.remote_window +|= add; 1949 } 1950 1951 fn discardChannelData(conn: *Conn, ch: *Channel, packet: []const u8, extended: bool) !void { 1952 // byte SSH_MSG_CHANNEL_DATA/EXTENDED_DATA 1953 // uint32 recipient_channel 1954 // [if extended: uint32 type_code] 1955 // string data 1956 const payload = try parseChannelData(packet, extended, ch.local_id); 1957 const data_len: u32 = @intCast(payload.len); 1958 1959 if (data_len > ch.local_window) return error.WindowExceeded; 1960 ch.local_window -= data_len; 1961 try maybeRefillRecvWindow(conn, ch, initial_recv_window); 1962 } 1963 1964 pub fn parseChannelData(packet: []const u8, extended: bool, expected_channel: u32) ![]const u8 { 1965 if (packet.len < 1) return error.EmptyPacket; 1966 var r = std.Io.Reader.fixed(packet[1..]); 1967 const recipient_channel = try r.takeInt(u32, .big); 1968 if (recipient_channel != expected_channel) return error.UnknownChannel; 1969 if (extended) { 1970 const data_type = try r.takeInt(u32, .big); 1971 if (data_type != SSH_EXTENDED_DATA_STDERR) return error.UnsupportedExtendedData; 1972 } 1973 const data_len = try r.takeInt(u32, .big); 1974 // catches both data_len > remaining (would otherwise be a bounds-check 1975 // panic on the slice) and trailing junk after the declared payload. 1976 if (data_len != r.buffered().len) return error.InvalidChannelData; 1977 return r.buffered(); 1978 } 1979 1980 fn parseChannelId(packet: []const u8, expected_channel: u32) !void { 1981 if (packet.len < 1) return error.EmptyPacket; 1982 var r = std.Io.Reader.fixed(packet[1..]); 1983 const recipient_channel = try r.takeInt(u32, .big); 1984 if (recipient_channel != expected_channel) return error.UnknownChannel; 1985 if (r.buffered().len != 0) return error.InvalidChannelMessage; 1986 } 1987 1988 /// send a CHANNEL_WINDOW_ADJUST if our receive window has dipped below half 1989 /// of `budget`, the credit we're willing to have outstanding. used both for 1990 /// the pre-session discard path and by SessionCtx after it consumes a 1991 /// CHANNEL_DATA payload. 1992 fn maybeRefillRecvWindow(conn: *Conn, ch: *Channel, budget: u32) !void { 1993 if (ch.local_window >= budget / 2) return; 1994 const add = budget - ch.local_window; 1995 ch.local_window += add; 1996 var adj: std.ArrayList(u8) = .empty; 1997 defer adj.deinit(conn.allocator); 1998 try adj.append(conn.allocator, SSH_MSG_CHANNEL_WINDOW_ADJUST); 1999 try writeU32(&adj, conn.allocator, ch.remote_id); 2000 try writeU32(&adj, conn.allocator, add); 2001 try conn.sc_cipher.writePacket(conn.io, conn.writer, adj.items); 2002 } 2003 2004 pub fn writeU32(buf: *std.ArrayList(u8), allocator: std.mem.Allocator, value: u32) !void { 2005 var bytes: [4]u8 = undefined; 2006 std.mem.writeInt(u32, &bytes, value, .big); 2007 try buf.appendSlice(allocator, &bytes); 2008 } 2009