╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_ssh_protocol.zig ───────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1827 try sendChannelOpenFailure(conn, packet, SSH_OPEN_UNKNOWN_CHANNEL_TYPE, "only session channels are supported"); 1828 return null; 1829 } 1830 1831 // a zero max_packet would leave writeChannel looping without progress 1832 if (max_packet == 0) { 1833 try sendChannelOpenFailure(conn, packet, SSH_OPEN_RESOURCE_SHORTAGE, "max packet size must be non-zero"); 1834 return null; 1835 } 1836 1837 const local_id: u32 = 0; // single-channel connection — id is always 0 from our side 1838 var reply: std.ArrayList(u8) = .empty; 1839 defer reply.deinit(allocator); 1840 try reply.append(allocator, SSH_MSG_CHANNEL_OPEN_CONFIRMATION); 1841 try writeU32(&reply, allocator, remote_id); 1842 try writeU32(&reply, allocator, local_id); 1843 try writeU32(&reply, allocator, initial_recv_window); 1844 try writeU32(&reply, allocator, max_packet_size); 1845 try conn.sc_cipher.writePacket(conn.io, conn.writer, reply.items); 1846 1847 return .{ 1848 .local_id = local_id, 1849 .remote_id = remote_id, 1850 .local_window = initial_recv_window, 1851 .remote_window = initial_window, 1852 .max_packet = max_packet, 1853 }; 1854 } 1855 1856 fn sendChannelOpenFailure(conn: *Conn, open_packet: []const u8, reason: u32, description: []const u8) !void { 1857 const allocator = conn.allocator; 1858 1859 // recover the sender_channel field from the CHANNEL_OPEN packet so we 1860 // address the failure to the correct id 1861 var r = std.Io.Reader.fixed(open_packet[1..]); 1862 const ch_type_len = try r.takeInt(u32, .big); 1863 try r.discardAll(ch_type_len); 1864 const remote_id = try r.takeInt(u32, .big); 1865 1866 var reply: std.ArrayList(u8) = .empty; 1867 defer reply.deinit(allocator); 1868 try reply.append(allocator, SSH_MSG_CHANNEL_OPEN_FAILURE); 1869 try writeU32(&reply, allocator, remote_id); 1870 try writeU32(&reply, allocator, reason); 1871 try writeStringField(&reply, allocator, description); 1872 try writeStringField(&reply, allocator, ""); // language tag 1873 try conn.sc_cipher.writePacket(conn.io, conn.writer, reply.items); 1874 } 1875 1876 /// returns the session request if this one triggered it, else null. an exec 1877 /// command is owned by `pending_exec`, which outlives the packet it arrived in. 1878 fn handleChannelRequest(conn: *Conn, ch: *Channel, packet: []const u8, pending_exec: *?[]u8) !?Request { 1879 const allocator = conn.allocator; 1880 1881 // byte SSH_MSG_CHANNEL_REQUEST 1882 // uint32 recipient_channel (== our local_id) 1883 // string request_type 1884 // boolean want_reply 1885 // …request-specific data 1886 var r = std.Io.Reader.fixed(packet[1..]); 1887 const recipient_channel = try r.takeInt(u32, .big); 1888 if (recipient_channel != ch.local_id) return error.UnknownChannel; 1889 const req_type = try takeStringField(allocator, &r, 64); 1890 defer allocator.free(req_type); 1891 const want_reply = (try r.takeByte()) != 0; 1892 1893 if (std.mem.eql(u8, req_type, "pty-req")) { 1894 // string TERM, uint32 width chars, uint32 height rows, uint32 width px, 1895 // uint32 height px, string modes 1896 const term = try takeStringField(allocator, &r, 64); 1897 defer allocator.free(term); 1898 ch.pty = try takePtySize(&r); 1899 try replyChannelRequest(conn, ch, want_reply, true); 1900 return null; 1901 } 1902 1903 if (std.mem.eql(u8, req_type, "env")) { 1904 // ignore environment variables silently (we don't pass them anywhere) 1905 try replyChannelRequest(conn, ch, want_reply, true); 1906 return null; 1907 } 1908 1909 if (std.mem.eql(u8, req_type, "window-change")) { 1910 // uint32 width cells, uint32 height rows, uint32 width px, uint32 height px 1911 const sz = try takePtySize(&r); 1912 if (ch.pty != null) ch.pty = sz; 1913 // spec: window-change MUST NOT request a reply, but accept either 1914 try replyChannelRequest(conn, ch, want_reply, true); 1915 return null; 1916 } 1917 1918 if (std.mem.eql(u8, req_type, "shell")) { 1919 try replyChannelRequest(conn, ch, want_reply, true); 1920 return .{ .shell = ch.pty }; 1921 } 1922 1923 if (std.mem.eql(u8, req_type, "exec")) { 1924 // capture the command string for the handler to inspect 1925 const cmd = try takeStringField(allocator, &r, 4096); 1926 if (pending_exec.*) |old| allocator.free(old); 1927 pending_exec.* = cmd; 1928 try replyChannelRequest(conn, ch, want_reply, true); 1929 return .{ .exec = cmd }; 1930 } 1931 1932 // unknown request kind — fail it 1933 try replyChannelRequest(conn, ch, want_reply, false); 1934 return null; 1935 } 1936 1937 fn replyChannelRequest(conn: *Conn, ch: *Channel, want_reply: bool, success: bool) !void { 1938 if (!want_reply) return; 1939 var reply: std.ArrayList(u8) = .empty; 1940 defer reply.deinit(conn.allocator); 1941 try reply.append(conn.allocator, if (success) SSH_MSG_CHANNEL_SUCCESS else SSH_MSG_CHANNEL_FAILURE); 1942 try writeU32(&reply, conn.allocator, ch.remote_id); 1943 try conn.sc_cipher.writePacket(conn.io, conn.writer, reply.items); 1944 } 1945 1946 fn handleWindowAdjust(ch: *Channel, packet: []const u8) !void { 1947 // byte SSH_MSG_CHANNEL_WINDOW_ADJUST 1948 // uint32 recipient_channel 1949 // uint32 bytes_to_add 1950 var r = std.Io.Reader.fixed(packet[1..]); 1951 const recipient_channel = try r.takeInt(u32, .big); 1952 if (recipient_channel != ch.local_id) return error.UnknownChannel; 1953 const add = try r.takeInt(u32, .big); 1954 ch.remote_window +|= add; 1955 } 1956 1957 // charge a payload's bytes against the receive window without keeping them. 1958 // the caller credits the window back, choosing the budget that suits it. 1959 fn discardChannelData(ch: *Channel, packet: []const u8, extended: bool) !void { 1960 // byte SSH_MSG_CHANNEL_DATA/EXTENDED_DATA 1961 // uint32 recipient_channel 1962 // [if extended: uint32 type_code] 1963 // string data 1964 const payload = try parseChannelData(packet, extended, ch.local_id); 1965 const data_len: u32 = @intCast(payload.len); 1966 1967 if (data_len > ch.local_window) return error.WindowExceeded; 1968 ch.local_window -= data_len; 1969 } 1970 1971 pub fn parseChannelData(packet: []const u8, extended: bool, expected_channel: u32) ![]const u8 { 1972 if (packet.len < 1) return error.EmptyPacket; 1973 var r = std.Io.Reader.fixed(packet[1..]); 1974 const recipient_channel = try r.takeInt(u32, .big); 1975 if (recipient_channel != expected_channel) return error.UnknownChannel; 1976 if (extended) { 1977 const data_type = try r.takeInt(u32, .big); 1978 if (data_type != SSH_EXTENDED_DATA_STDERR) return error.UnsupportedExtendedData; 1979 } 1980 const data_len = try r.takeInt(u32, .big); 1981 // catches both data_len > remaining (would otherwise be a bounds-check 1982 // panic on the slice) and trailing junk after the declared payload. 1983 if (data_len != r.buffered().len) return error.InvalidChannelData; 1984 return r.buffered(); 1985 } 1986 1987 fn parseChannelId(packet: []const u8, expected_channel: u32) !void { 1988 if (packet.len < 1) return error.EmptyPacket; 1989 var r = std.Io.Reader.fixed(packet[1..]); 1990 const recipient_channel = try r.takeInt(u32, .big); 1991 if (recipient_channel != expected_channel) return error.UnknownChannel; 1992 if (r.buffered().len != 0) return error.InvalidChannelMessage; 1993 } 1994 1995 /// send a CHANNEL_WINDOW_ADJUST if our receive window has dipped below half 1996 /// of `budget`, the credit we're willing to have outstanding. used both for 1997 /// the pre-session discard path and by SessionCtx after it consumes a 1998 /// CHANNEL_DATA payload. 1999 fn maybeRefillRecvWindow(conn: *Conn, ch: *Channel, budget: u32) !void { 2000 if (ch.local_window >= budget / 2) return; 2001 const add = budget - ch.local_window; 2002 ch.local_window += add; 2003 var adj: std.ArrayList(u8) = .empty; 2004 defer adj.deinit(conn.allocator); 2005 try adj.append(conn.allocator, SSH_MSG_CHANNEL_WINDOW_ADJUST); 2006 try writeU32(&adj, conn.allocator, ch.remote_id); 2007 try writeU32(&adj, conn.allocator, add); 2008 try conn.sc_cipher.writePacket(conn.io, conn.writer, adj.items); 2009 } 2010 2011 pub fn writeU32(buf: *std.ArrayList(u8), allocator: std.mem.Allocator, value: u32) !void { 2012 var bytes: [4]u8 = undefined; 2013 std.mem.writeInt(u32, &bytes, value, .big); 2014 try buf.appendSlice(allocator, &bytes); 2015 } 2016