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