╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_ssh_protocol.zig ───────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1836 // uint32 height px, string modes 1837 const term = try takeStringField(allocator, &r, 64); 1838 defer allocator.free(term); 1839 ch.pty = try takePtySize(&r); 1840 try replyChannelRequest(io, allocator, sc_cipher, writer, ch, want_reply, true); 1841 return null; 1842 } 1843 1844 if (std.mem.eql(u8, req_type, "env")) { 1845 // ignore environment variables silently (we don't pass them anywhere) 1846 try replyChannelRequest(io, allocator, sc_cipher, writer, ch, want_reply, true); 1847 return null; 1848 } 1849 1850 if (std.mem.eql(u8, req_type, "window-change")) { 1851 // uint32 width cells, uint32 height rows, uint32 width px, uint32 height px 1852 const sz = try takePtySize(&r); 1853 if (ch.pty != null) ch.pty = sz; 1854 // spec: window-change MUST NOT request a reply, but accept either 1855 try replyChannelRequest(io, allocator, sc_cipher, writer, ch, want_reply, true); 1856 return null; 1857 } 1858 1859 if (std.mem.eql(u8, req_type, "shell")) { 1860 try replyChannelRequest(io, allocator, sc_cipher, writer, ch, want_reply, true); 1861 return .shell; 1862 } 1863 1864 if (std.mem.eql(u8, req_type, "exec")) { 1865 // capture the command string for the handler to inspect 1866 const cmd = try takeStringField(allocator, &r, 4096); 1867 if (pending_exec.*) |old| allocator.free(old); 1868 pending_exec.* = cmd; 1869 try replyChannelRequest(io, allocator, sc_cipher, writer, ch, want_reply, true); 1870 return .exec; 1871 } 1872 1873 // unknown request kind — fail it 1874 try replyChannelRequest(io, allocator, sc_cipher, writer, ch, want_reply, false); 1875 return null; 1876 } 1877 1878 fn replyChannelRequest( 1879 io: std.Io, 1880 allocator: std.mem.Allocator, 1881 sc_cipher: *Cipher, 1882 writer: *std.Io.Writer, 1883 ch: *Channel, 1884 want_reply: bool, 1885 success: bool, 1886 ) !void { 1887 if (!want_reply) return; 1888 var reply: std.ArrayList(u8) = .empty; 1889 defer reply.deinit(allocator); 1890 try reply.append(allocator, if (success) SSH_MSG_CHANNEL_SUCCESS else SSH_MSG_CHANNEL_FAILURE); 1891 try writeU32(&reply, allocator, ch.remote_id); 1892 try sc_cipher.writePacket(io, writer, reply.items); 1893 } 1894 1895 fn handleWindowAdjust(ch: *Channel, packet: []const u8) !void { 1896 // byte SSH_MSG_CHANNEL_WINDOW_ADJUST 1897 // uint32 recipient_channel 1898 // uint32 bytes_to_add 1899 var r = std.Io.Reader.fixed(packet[1..]); 1900 const recipient_channel = try r.takeInt(u32, .big); 1901 if (recipient_channel != ch.local_id) return error.UnknownChannel; 1902 const add = try r.takeInt(u32, .big); 1903 ch.remote_window +|= add; 1904 } 1905 1906 fn discardChannelData( 1907 io: std.Io, 1908 allocator: std.mem.Allocator, 1909 sc_cipher: *Cipher, 1910 writer: *std.Io.Writer, 1911 ch: *Channel, 1912 packet: []const u8, 1913 extended: bool, 1914 ) !void { 1915 // byte SSH_MSG_CHANNEL_DATA/EXTENDED_DATA 1916 // uint32 recipient_channel 1917 // [if extended: uint32 type_code] 1918 // string data 1919 const payload = try parseChannelData(packet, extended, ch.local_id); 1920 const data_len: u32 = @intCast(payload.len); 1921 1922 if (data_len > ch.local_window) return error.WindowExceeded; 1923 ch.local_window -= data_len; 1924 try maybeRefillRecvWindow(io, allocator, sc_cipher, writer, ch); 1925 } 1926 1927 pub fn parseChannelData(packet: []const u8, extended: bool, expected_channel: u32) ![]const u8 { 1928 if (packet.len < 1) return error.EmptyPacket; 1929 var r = std.Io.Reader.fixed(packet[1..]); 1930 const recipient_channel = try r.takeInt(u32, .big); 1931 if (recipient_channel != expected_channel) return error.UnknownChannel; 1932 if (extended) { 1933 const data_type = try r.takeInt(u32, .big); 1934 if (data_type != SSH_EXTENDED_DATA_STDERR) return error.UnsupportedExtendedData; 1935 } 1936 const data_len = try r.takeInt(u32, .big); 1937 // catches both data_len > remaining (would otherwise be a bounds-check 1938 // panic on the slice) and trailing junk after the declared payload. 1939 if (data_len != r.buffered().len) return error.InvalidChannelData; 1940 return r.buffered(); 1941 } 1942 1943 fn parseChannelId(packet: []const u8, expected_channel: u32) !void { 1944 if (packet.len < 1) return error.EmptyPacket; 1945 var r = std.Io.Reader.fixed(packet[1..]); 1946 const recipient_channel = try r.takeInt(u32, .big); 1947 if (recipient_channel != expected_channel) return error.UnknownChannel; 1948 if (r.buffered().len != 0) return error.InvalidChannelMessage; 1949 } 1950 1951 /// Send a CHANNEL_WINDOW_ADJUST if our receive window has dipped below half 1952 /// of the initial value. Used both for the pre-session discard path and by 1953 /// SessionCtx after it consumes a CHANNEL_DATA payload. 1954 fn maybeRefillRecvWindow( 1955 io: std.Io, 1956 allocator: std.mem.Allocator, 1957 sc_cipher: *Cipher, 1958 writer: *std.Io.Writer, 1959 ch: *Channel, 1960 ) !void { 1961 if (ch.local_window >= initial_recv_window / 2) return; 1962 const add = initial_recv_window - ch.local_window; 1963 ch.local_window += add; 1964 var adj: std.ArrayList(u8) = .empty; 1965 defer adj.deinit(allocator); 1966 try adj.append(allocator, SSH_MSG_CHANNEL_WINDOW_ADJUST); 1967 try writeU32(&adj, allocator, ch.remote_id); 1968 try writeU32(&adj, allocator, add); 1969 try sc_cipher.writePacket(io, writer, adj.items); 1970 } 1971 1972 pub fn writeU32(buf: *std.ArrayList(u8), allocator: std.mem.Allocator, value: u32) !void { 1973 var bytes: [4]u8 = undefined; 1974 std.mem.writeInt(u32, &bytes, value, .big); 1975 try buf.appendSlice(allocator, &bytes); 1976 } 1977