╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_ssh_protocol.zig ───────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1774 try writeStringField(&reply, allocator, description); 1775 try writeStringField(&reply, allocator, ""); // language tag 1776 try conn.sc_cipher.writePacket(conn.io, conn.writer, reply.items); 1777 } 1778 1779 const RequestKind = enum { shell, exec }; 1780 1781 /// Returns the kind of session request if this triggered one, else null. 1782 fn handleChannelRequest(conn: *Conn, ch: *Channel, packet: []const u8, pending_exec: *?[]u8) !?RequestKind { 1783 const allocator = conn.allocator; 1784 1785 // byte SSH_MSG_CHANNEL_REQUEST 1786 // uint32 recipient_channel (== our local_id) 1787 // string request_type 1788 // boolean want_reply 1789 // …request-specific data 1790 var r = std.Io.Reader.fixed(packet[1..]); 1791 const recipient_channel = try r.takeInt(u32, .big); 1792 if (recipient_channel != ch.local_id) return error.UnknownChannel; 1793 const req_type = try takeStringField(allocator, &r, 64); 1794 defer allocator.free(req_type); 1795 const want_reply = (try r.takeByte()) != 0; 1796 1797 if (std.mem.eql(u8, req_type, "pty-req")) { 1798 // string TERM, uint32 width chars, uint32 height rows, uint32 width px, 1799 // uint32 height px, string modes 1800 const term = try takeStringField(allocator, &r, 64); 1801 defer allocator.free(term); 1802 ch.pty = try takePtySize(&r); 1803 try replyChannelRequest(conn, ch, want_reply, true); 1804 return null; 1805 } 1806 1807 if (std.mem.eql(u8, req_type, "env")) { 1808 // ignore environment variables silently (we don't pass them anywhere) 1809 try replyChannelRequest(conn, ch, want_reply, true); 1810 return null; 1811 } 1812 1813 if (std.mem.eql(u8, req_type, "window-change")) { 1814 // uint32 width cells, uint32 height rows, uint32 width px, uint32 height px 1815 const sz = try takePtySize(&r); 1816 if (ch.pty != null) ch.pty = sz; 1817 // spec: window-change MUST NOT request a reply, but accept either 1818 try replyChannelRequest(conn, ch, want_reply, true); 1819 return null; 1820 } 1821 1822 if (std.mem.eql(u8, req_type, "shell")) { 1823 try replyChannelRequest(conn, ch, want_reply, true); 1824 return .shell; 1825 } 1826 1827 if (std.mem.eql(u8, req_type, "exec")) { 1828 // capture the command string for the handler to inspect 1829 const cmd = try takeStringField(allocator, &r, 4096); 1830 if (pending_exec.*) |old| allocator.free(old); 1831 pending_exec.* = cmd; 1832 try replyChannelRequest(conn, ch, want_reply, true); 1833 return .exec; 1834 } 1835 1836 // unknown request kind — fail it 1837 try replyChannelRequest(conn, ch, want_reply, false); 1838 return null; 1839 } 1840 1841 fn replyChannelRequest(conn: *Conn, ch: *Channel, want_reply: bool, success: bool) !void { 1842 if (!want_reply) return; 1843 var reply: std.ArrayList(u8) = .empty; 1844 defer reply.deinit(conn.allocator); 1845 try reply.append(conn.allocator, if (success) SSH_MSG_CHANNEL_SUCCESS else SSH_MSG_CHANNEL_FAILURE); 1846 try writeU32(&reply, conn.allocator, ch.remote_id); 1847 try conn.sc_cipher.writePacket(conn.io, conn.writer, reply.items); 1848 } 1849 1850 fn handleWindowAdjust(ch: *Channel, packet: []const u8) !void { 1851 // byte SSH_MSG_CHANNEL_WINDOW_ADJUST 1852 // uint32 recipient_channel 1853 // uint32 bytes_to_add 1854 var r = std.Io.Reader.fixed(packet[1..]); 1855 const recipient_channel = try r.takeInt(u32, .big); 1856 if (recipient_channel != ch.local_id) return error.UnknownChannel; 1857 const add = try r.takeInt(u32, .big); 1858 ch.remote_window +|= add; 1859 } 1860 1861 fn discardChannelData(conn: *Conn, ch: *Channel, packet: []const u8, extended: bool) !void { 1862 // byte SSH_MSG_CHANNEL_DATA/EXTENDED_DATA 1863 // uint32 recipient_channel 1864 // [if extended: uint32 type_code] 1865 // string data 1866 const payload = try parseChannelData(packet, extended, ch.local_id); 1867 const data_len: u32 = @intCast(payload.len); 1868 1869 if (data_len > ch.local_window) return error.WindowExceeded; 1870 ch.local_window -= data_len; 1871 try maybeRefillRecvWindow(conn, ch); 1872 } 1873 1874 pub fn parseChannelData(packet: []const u8, extended: bool, expected_channel: u32) ![]const u8 { 1875 if (packet.len < 1) return error.EmptyPacket; 1876 var r = std.Io.Reader.fixed(packet[1..]); 1877 const recipient_channel = try r.takeInt(u32, .big); 1878 if (recipient_channel != expected_channel) return error.UnknownChannel; 1879 if (extended) { 1880 const data_type = try r.takeInt(u32, .big); 1881 if (data_type != SSH_EXTENDED_DATA_STDERR) return error.UnsupportedExtendedData; 1882 } 1883 const data_len = try r.takeInt(u32, .big); 1884 // catches both data_len > remaining (would otherwise be a bounds-check 1885 // panic on the slice) and trailing junk after the declared payload. 1886 if (data_len != r.buffered().len) return error.InvalidChannelData; 1887 return r.buffered(); 1888 } 1889 1890 fn parseChannelId(packet: []const u8, expected_channel: u32) !void { 1891 if (packet.len < 1) return error.EmptyPacket; 1892 var r = std.Io.Reader.fixed(packet[1..]); 1893 const recipient_channel = try r.takeInt(u32, .big); 1894 if (recipient_channel != expected_channel) return error.UnknownChannel; 1895 if (r.buffered().len != 0) return error.InvalidChannelMessage; 1896 } 1897 1898 /// Send a CHANNEL_WINDOW_ADJUST if our receive window has dipped below half 1899 /// of the initial value. Used both for the pre-session discard path and by 1900 /// SessionCtx after it consumes a CHANNEL_DATA payload. 1901 fn maybeRefillRecvWindow(conn: *Conn, ch: *Channel) !void { 1902 if (ch.local_window >= initial_recv_window / 2) return; 1903 const add = initial_recv_window - ch.local_window; 1904 ch.local_window += add; 1905 var adj: std.ArrayList(u8) = .empty; 1906 defer adj.deinit(conn.allocator); 1907 try adj.append(conn.allocator, SSH_MSG_CHANNEL_WINDOW_ADJUST); 1908 try writeU32(&adj, conn.allocator, ch.remote_id); 1909 try writeU32(&adj, conn.allocator, add); 1910 try conn.sc_cipher.writePacket(conn.io, conn.writer, adj.items); 1911 } 1912 1913 pub fn writeU32(buf: *std.ArrayList(u8), allocator: std.mem.Allocator, value: u32) !void { 1914 var bytes: [4]u8 = undefined; 1915 std.mem.writeInt(u32, &bytes, value, .big); 1916 try buf.appendSlice(allocator, &bytes); 1917 } 1918