╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_ssh_protocol.zig ───────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1835 try writeU32(&reply, allocator, remote_id); 1836 try writeU32(&reply, allocator, local_id); 1837 try writeU32(&reply, allocator, initial_recv_window); 1838 try writeU32(&reply, allocator, max_packet_size); 1839 try conn.sc_cipher.writePacket(conn.io, conn.writer, reply.items); 1840 1841 return .{ 1842 .local_id = local_id, 1843 .remote_id = remote_id, 1844 .local_window = initial_recv_window, 1845 .remote_window = initial_window, 1846 .max_packet = max_packet, 1847 }; 1848 } 1849 1850 fn sendChannelOpenFailure(conn: *Conn, open_packet: []const u8, reason: u32, description: []const u8) !void { 1851 const allocator = conn.allocator; 1852 1853 // recover the sender_channel field from the CHANNEL_OPEN packet so we 1854 // address the failure to the correct id 1855 var r = std.Io.Reader.fixed(open_packet[1..]); 1856 const ch_type_len = try r.takeInt(u32, .big); 1857 try r.discardAll(ch_type_len); 1858 const remote_id = try r.takeInt(u32, .big); 1859 1860 var reply: std.ArrayList(u8) = .empty; 1861 defer reply.deinit(allocator); 1862 try reply.append(allocator, SSH_MSG_CHANNEL_OPEN_FAILURE); 1863 try writeU32(&reply, allocator, remote_id); 1864 try writeU32(&reply, allocator, reason); 1865 try writeStringField(&reply, allocator, description); 1866 try writeStringField(&reply, allocator, ""); // language tag 1867 try conn.sc_cipher.writePacket(conn.io, conn.writer, reply.items); 1868 } 1869 1870 /// returns the session request if this one triggered it, else null. an exec 1871 /// command is owned by `pending_exec`, which outlives the packet it arrived in. 1872 fn handleChannelRequest(conn: *Conn, ch: *Channel, packet: []const u8, pending_exec: *?[]u8) !?Request { 1873 const allocator = conn.allocator; 1874 1875 // byte SSH_MSG_CHANNEL_REQUEST 1876 // uint32 recipient_channel (== our local_id) 1877 // string request_type 1878 // boolean want_reply 1879 // …request-specific data 1880 var r = std.Io.Reader.fixed(packet[1..]); 1881 const recipient_channel = try r.takeInt(u32, .big); 1882 if (recipient_channel != ch.local_id) return error.UnknownChannel; 1883 const req_type = try takeStringField(allocator, &r, 64); 1884 defer allocator.free(req_type); 1885 const want_reply = (try r.takeByte()) != 0; 1886 1887 if (std.mem.eql(u8, req_type, "pty-req")) { 1888 // string TERM, uint32 width chars, uint32 height rows, uint32 width px, 1889 // uint32 height px, string modes 1890 const term = try takeStringField(allocator, &r, 64); 1891 defer allocator.free(term); 1892 ch.pty = try takePtySize(&r); 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