╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_ssh_protocol.zig ───────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1680 } 1681 1682 if (std.mem.eql(u8, req_type, "shell")) { 1683 try replyChannelRequest(io, allocator, sc_cipher, writer, ch, want_reply, true); 1684 return .shell; 1685 } 1686 1687 if (std.mem.eql(u8, req_type, "exec")) { 1688 // capture the command string for the handler to inspect 1689 const cmd = try takeStringField(allocator, &r, 4096); 1690 if (pending_exec.*) |old| allocator.free(old); 1691 pending_exec.* = cmd; 1692 try replyChannelRequest(io, allocator, sc_cipher, writer, ch, want_reply, true); 1693 return .exec; 1694 } 1695 1696 // unknown request kind — fail it 1697 try replyChannelRequest(io, allocator, sc_cipher, writer, ch, want_reply, false); 1698 return null; 1699 } 1700 1701 fn replyChannelRequest( 1702 io: std.Io, 1703 allocator: std.mem.Allocator, 1704 sc_cipher: *Cipher, 1705 writer: *std.Io.Writer, 1706 ch: *Channel, 1707 want_reply: bool, 1708 success: bool, 1709 ) !void { 1710 if (!want_reply) return; 1711 var reply: std.ArrayList(u8) = .empty; 1712 defer reply.deinit(allocator); 1713 try reply.append(allocator, if (success) SSH_MSG_CHANNEL_SUCCESS else SSH_MSG_CHANNEL_FAILURE); 1714 try writeU32(&reply, allocator, ch.remote_id); 1715 try sc_cipher.writePacket(io, allocator, writer, reply.items); 1716 } 1717 1718 fn handleWindowAdjust(ch: *Channel, packet: []const u8) !void { 1719 // byte SSH_MSG_CHANNEL_WINDOW_ADJUST 1720 // uint32 recipient_channel 1721 // uint32 bytes_to_add 1722 var r = std.Io.Reader.fixed(packet[1..]); 1723 const recipient_channel = try r.takeInt(u32, .big); 1724 if (recipient_channel != ch.local_id) return error.UnknownChannel; 1725 const add = try r.takeInt(u32, .big); 1726 ch.remote_window +|= add; 1727 } 1728 1729 fn discardChannelData( 1730 io: std.Io, 1731 allocator: std.mem.Allocator, 1732 sc_cipher: *Cipher, 1733 writer: *std.Io.Writer, 1734 ch: *Channel, 1735 packet: []const u8, 1736 extended: bool, 1737 ) !void { 1738 // byte SSH_MSG_CHANNEL_DATA/EXTENDED_DATA 1739 // uint32 recipient_channel 1740 // [if extended: uint32 type_code] 1741 // string data 1742 const payload = try parseChannelData(packet, extended, ch.local_id); 1743 const data_len: u32 = @intCast(payload.len); 1744 1745 if (data_len > ch.local_window) return error.WindowExceeded; 1746 ch.local_window -= data_len; 1747 try maybeRefillRecvWindow(io, allocator, sc_cipher, writer, ch); 1748 } 1749 1750 pub fn parseChannelData(packet: []const u8, extended: bool, expected_channel: u32) ![]const u8 { 1751 if (packet.len < 1) return error.EmptyPacket; 1752 var r = std.Io.Reader.fixed(packet[1..]); 1753 const recipient_channel = try r.takeInt(u32, .big); 1754 if (recipient_channel != expected_channel) return error.UnknownChannel; 1755 if (extended) { 1756 const data_type = try r.takeInt(u32, .big); 1757 if (data_type != SSH_EXTENDED_DATA_STDERR) return error.UnsupportedExtendedData; 1758 } 1759 const data_len = try r.takeInt(u32, .big); 1760 // catches both data_len > remaining (would otherwise be a bounds-check 1761 // panic on the slice) and trailing junk after the declared payload. 1762 if (data_len != r.buffered().len) return error.InvalidChannelData; 1763 return r.buffered(); 1764 } 1765 1766 fn parseChannelId(packet: []const u8, expected_channel: u32) !void { 1767 if (packet.len < 1) return error.EmptyPacket; 1768 var r = std.Io.Reader.fixed(packet[1..]); 1769 const recipient_channel = try r.takeInt(u32, .big); 1770 if (recipient_channel != expected_channel) return error.UnknownChannel; 1771 if (r.buffered().len != 0) return error.InvalidChannelMessage; 1772 } 1773 1774 /// Send a CHANNEL_WINDOW_ADJUST if our receive window has dipped below half 1775 /// of the initial value. Used both for the pre-session discard path and by 1776 /// SessionCtx after it consumes a CHANNEL_DATA payload. 1777 fn maybeRefillRecvWindow( 1778 io: std.Io, 1779 allocator: std.mem.Allocator, 1780 sc_cipher: *Cipher, 1781 writer: *std.Io.Writer, 1782 ch: *Channel, 1783 ) !void { 1784 if (ch.local_window >= initial_recv_window / 2) return; 1785 const add = initial_recv_window - ch.local_window; 1786 ch.local_window += add; 1787 var adj: std.ArrayList(u8) = .empty; 1788 defer adj.deinit(allocator); 1789 try adj.append(allocator, SSH_MSG_CHANNEL_WINDOW_ADJUST); 1790 try writeU32(&adj, allocator, ch.remote_id); 1791 try writeU32(&adj, allocator, add); 1792 try sc_cipher.writePacket(io, allocator, writer, adj.items); 1793 } 1794 1795 pub fn writeU32(buf: *std.ArrayList(u8), allocator: std.mem.Allocator, value: u32) !void { 1796 var bytes: [4]u8 = undefined; 1797 std.mem.writeInt(u32, &bytes, value, .big); 1798 try buf.appendSlice(allocator, &bytes); 1799 } 1800