╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_ssh_protocol.zig ───────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1614 description: []const u8, 1615 ) !void { 1616 // recover the sender_channel field from the CHANNEL_OPEN packet so we 1617 // address the failure to the correct id 1618 var r = std.Io.Reader.fixed(open_packet[1..]); 1619 const ch_type_len = try r.takeInt(u32, .big); 1620 try r.discardAll(ch_type_len); 1621 const remote_id = try r.takeInt(u32, .big); 1622 1623 var reply: std.ArrayList(u8) = .empty; 1624 defer reply.deinit(allocator); 1625 try reply.append(allocator, SSH_MSG_CHANNEL_OPEN_FAILURE); 1626 try writeU32(&reply, allocator, remote_id); 1627 try writeU32(&reply, allocator, reason); 1628 try writeStringField(&reply, allocator, description); 1629 try writeStringField(&reply, allocator, ""); // language tag 1630 try sc_cipher.writePacket(io, allocator, writer, reply.items); 1631 } 1632 1633 const RequestKind = enum { shell, exec }; 1634 1635 /// Returns the kind of session request if this triggered one, else null. 1636 fn handleChannelRequest( 1637 io: std.Io, 1638 allocator: std.mem.Allocator, 1639 sc_cipher: *Cipher, 1640 writer: *std.Io.Writer, 1641 ch: *Channel, 1642 packet: []const u8, 1643 pending_exec: *?[]u8, 1644 ) !?RequestKind { 1645 // byte SSH_MSG_CHANNEL_REQUEST 1646 // uint32 recipient_channel (== our local_id) 1647 // string request_type 1648 // boolean want_reply 1649 // …request-specific data 1650 var r = std.Io.Reader.fixed(packet[1..]); 1651 const recipient_channel = try r.takeInt(u32, .big); 1652 if (recipient_channel != ch.local_id) return error.UnknownChannel; 1653 const req_type = try takeStringField(allocator, &r, 64); 1654 defer allocator.free(req_type); 1655 const want_reply = (try r.takeByte()) != 0; 1656 1657 if (std.mem.eql(u8, req_type, "pty-req")) { 1658 // string TERM, uint32 width chars, uint32 height rows, uint32 width px, 1659 // uint32 height px, string modes 1660 const term = try takeStringField(allocator, &r, 64); 1661 defer allocator.free(term); 1662 const width = try r.takeInt(u32, .big); 1663 const height = try r.takeInt(u32, .big); 1664 ch.pty = .{ .width_cells = @intCast(@min(width, 0xFFFF)), .height_cells = @intCast(@min(height, 0xFFFF)) }; 1665 try replyChannelRequest(io, allocator, sc_cipher, writer, ch, want_reply, true); 1666 return null; 1667 } 1668 1669 if (std.mem.eql(u8, req_type, "env")) { 1670 // ignore environment variables silently (we don't pass them anywhere) 1671 try replyChannelRequest(io, allocator, sc_cipher, writer, ch, want_reply, true); 1672 return null; 1673 } 1674 1675 if (std.mem.eql(u8, req_type, "window-change")) { 1676 // uint32 width cells, uint32 height rows, uint32 width px, uint32 height px 1677 const width = try r.takeInt(u32, .big); 1678 const height = try r.takeInt(u32, .big); 1679 if (ch.pty != null) { 1680 ch.pty = .{ .width_cells = @intCast(@min(width, 0xFFFF)), .height_cells = @intCast(@min(height, 0xFFFF)) }; 1681 } 1682 // spec: window-change MUST NOT request a reply, but accept either 1683 try replyChannelRequest(io, allocator, sc_cipher, writer, ch, want_reply, true); 1684 return null; 1685 } 1686 1687 if (std.mem.eql(u8, req_type, "shell")) { 1688 try replyChannelRequest(io, allocator, sc_cipher, writer, ch, want_reply, true); 1689 return .shell; 1690 } 1691 1692 if (std.mem.eql(u8, req_type, "exec")) { 1693 // capture the command string for the handler to inspect 1694 const cmd = try takeStringField(allocator, &r, 4096); 1695 if (pending_exec.*) |old| allocator.free(old); 1696 pending_exec.* = cmd; 1697 try replyChannelRequest(io, allocator, sc_cipher, writer, ch, want_reply, true); 1698 return .exec; 1699 } 1700 1701 // unknown request kind — fail it 1702 try replyChannelRequest(io, allocator, sc_cipher, writer, ch, want_reply, false); 1703 return null; 1704 } 1705 1706 fn replyChannelRequest( 1707 io: std.Io, 1708 allocator: std.mem.Allocator, 1709 sc_cipher: *Cipher, 1710 writer: *std.Io.Writer, 1711 ch: *Channel, 1712 want_reply: bool, 1713 success: bool, 1714 ) !void { 1715 if (!want_reply) return; 1716 var reply: std.ArrayList(u8) = .empty; 1717 defer reply.deinit(allocator); 1718 try reply.append(allocator, if (success) SSH_MSG_CHANNEL_SUCCESS else SSH_MSG_CHANNEL_FAILURE); 1719 try writeU32(&reply, allocator, ch.remote_id); 1720 try sc_cipher.writePacket(io, allocator, writer, reply.items); 1721 } 1722 1723 fn handleWindowAdjust(ch: *Channel, packet: []const u8) !void { 1724 // byte SSH_MSG_CHANNEL_WINDOW_ADJUST 1725 // uint32 recipient_channel 1726 // uint32 bytes_to_add 1727 var r = std.Io.Reader.fixed(packet[1..]); 1728 const recipient_channel = try r.takeInt(u32, .big); 1729 if (recipient_channel != ch.local_id) return error.UnknownChannel; 1730 const add = try r.takeInt(u32, .big); 1731 ch.remote_window +|= add; 1732 } 1733 1734 fn discardChannelData( 1735 io: std.Io, 1736 allocator: std.mem.Allocator, 1737 sc_cipher: *Cipher, 1738 writer: *std.Io.Writer, 1739 ch: *Channel, 1740 packet: []const u8, 1741 extended: bool, 1742 ) !void { 1743 // byte SSH_MSG_CHANNEL_DATA/EXTENDED_DATA 1744 // uint32 recipient_channel 1745 // [if extended: uint32 type_code] 1746 // string data 1747 const payload = try parseChannelData(packet, extended, ch.local_id); 1748 const data_len: u32 = @intCast(payload.len); 1749 1750 if (data_len > ch.local_window) return error.WindowExceeded; 1751 ch.local_window -= data_len; 1752 try maybeRefillRecvWindow(io, allocator, sc_cipher, writer, ch); 1753 } 1754 1755 pub fn parseChannelData(packet: []const u8, extended: bool, expected_channel: u32) ![]const u8 { 1756 if (packet.len < 1) return error.EmptyPacket; 1757 var r = std.Io.Reader.fixed(packet[1..]); 1758 const recipient_channel = try r.takeInt(u32, .big); 1759 if (recipient_channel != expected_channel) return error.UnknownChannel; 1760 if (extended) { 1761 const data_type = try r.takeInt(u32, .big); 1762 if (data_type != SSH_EXTENDED_DATA_STDERR) return error.UnsupportedExtendedData; 1763 } 1764 const data_len = try r.takeInt(u32, .big); 1765 // catches both data_len > remaining (would otherwise be a bounds-check 1766 // panic on the slice) and trailing junk after the declared payload. 1767 if (data_len != r.buffered().len) return error.InvalidChannelData; 1768 return r.buffered(); 1769 } 1770 1771 fn parseChannelId(packet: []const u8, expected_channel: u32) !void { 1772 if (packet.len < 1) return error.EmptyPacket; 1773 var r = std.Io.Reader.fixed(packet[1..]); 1774 const recipient_channel = try r.takeInt(u32, .big); 1775 if (recipient_channel != expected_channel) return error.UnknownChannel; 1776 if (r.buffered().len != 0) return error.InvalidChannelMessage; 1777 } 1778 1779 /// Send a CHANNEL_WINDOW_ADJUST if our receive window has dipped below half 1780 /// of the initial value. Used both for the pre-session discard path and by 1781 /// SessionCtx after it consumes a CHANNEL_DATA payload. 1782 fn maybeRefillRecvWindow( 1783 io: std.Io, 1784 allocator: std.mem.Allocator, 1785 sc_cipher: *Cipher, 1786 writer: *std.Io.Writer, 1787 ch: *Channel, 1788 ) !void { 1789 if (ch.local_window >= initial_recv_window / 2) return; 1790 const add = initial_recv_window - ch.local_window; 1791 ch.local_window += add; 1792 var adj: std.ArrayList(u8) = .empty; 1793 defer adj.deinit(allocator); 1794 try adj.append(allocator, SSH_MSG_CHANNEL_WINDOW_ADJUST); 1795 try writeU32(&adj, allocator, ch.remote_id); 1796 try writeU32(&adj, allocator, add); 1797 try sc_cipher.writePacket(io, allocator, writer, adj.items); 1798 } 1799 1800 pub fn writeU32(buf: *std.ArrayList(u8), allocator: std.mem.Allocator, value: u32) !void { 1801 var bytes: [4]u8 = undefined; 1802 std.mem.writeInt(u32, &bytes, value, .big); 1803 try buf.appendSlice(allocator, &bytes); 1804 } 1805