╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_ssh_protocol.zig ───────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1497 1498 SSH_MSG_CHANNEL_DATA, SSH_MSG_CHANNEL_EXTENDED_DATA => { 1499 const ch = if (channel) |*c| c else continue; 1500 // before shell/exec the client shouldn't be sending data, but 1501 // some clients are chatty; discard and refill window. 1502 try discardChannelData(io, allocator, sc_cipher, writer, ch, packet, msg_type == SSH_MSG_CHANNEL_EXTENDED_DATA); 1503 }, 1504 1505 SSH_MSG_CHANNEL_EOF => { 1506 if (channel) |*c| { 1507 try parseChannelId(packet, c.local_id); 1508 c.eof_received = true; 1509 } 1510 }, 1511 1512 SSH_MSG_CHANNEL_CLOSE => { 1513 if (channel) |*c| try parseChannelId(packet, c.local_id); 1514 return; 1515 }, 1516 1517 else => {}, // ignore unknown messages 1518 } 1519 } 1520 } 1521 1522 fn handleGlobalRequest( 1523 io: std.Io, 1524 allocator: std.mem.Allocator, 1525 sc_cipher: *Cipher, 1526 writer: *std.Io.Writer, 1527 packet: []const u8, 1528 ) !void { 1529 // byte SSH_MSG_GLOBAL_REQUEST 1530 // string request_name 1531 // boolean want_reply 1532 // …request-specific data we ignore 1533 var r = std.Io.Reader.fixed(packet[1..]); 1534 const name = try takeStringField(allocator, &r, 256); 1535 defer allocator.free(name); 1536 const want_reply = (try r.takeByte()) != 0; 1537 if (want_reply) { 1538 try sc_cipher.writePacket(io, allocator, writer, &[_]u8{SSH_MSG_REQUEST_FAILURE}); 1539 } 1540 } 1541 1542 /// Parse a CHANNEL_OPEN. On a session channel, send CHANNEL_OPEN_CONFIRMATION 1543 /// and return the new Channel. On other types, send CHANNEL_OPEN_FAILURE and 1544 /// return null. 1545 fn handleChannelOpen( 1546 io: std.Io, 1547 allocator: std.mem.Allocator, 1548 sc_cipher: *Cipher, 1549 writer: *std.Io.Writer, 1550 packet: []const u8, 1551 ) !?Channel { 1552 // byte SSH_MSG_CHANNEL_OPEN 1553 // string channel_type 1554 // uint32 sender_channel (peer's id) 1555 // uint32 initial_window 1556 // uint32 max_packet 1557 var r = std.Io.Reader.fixed(packet[1..]); 1558 const ch_type = try takeStringField(allocator, &r, 64); 1559 defer allocator.free(ch_type); 1560 const remote_id = try r.takeInt(u32, .big); 1561 const initial_window = try r.takeInt(u32, .big); 1562 const max_packet = try r.takeInt(u32, .big); 1563 1564 if (!std.mem.eql(u8, ch_type, "session")) { 1565 try sendChannelOpenFailure(io, allocator, sc_cipher, writer, packet, SSH_OPEN_UNKNOWN_CHANNEL_TYPE, "only session channels are supported"); 1566 return null; 1567 } 1568 1569 const local_id: u32 = 0; // single-channel connection — id is always 0 from our side 1570 var reply: std.ArrayList(u8) = .empty; 1571 defer reply.deinit(allocator); 1572 try reply.append(allocator, SSH_MSG_CHANNEL_OPEN_CONFIRMATION); 1573 try writeU32(&reply, allocator, remote_id); 1574 try writeU32(&reply, allocator, local_id); 1575 try writeU32(&reply, allocator, initial_recv_window); 1576 try writeU32(&reply, allocator, max_packet_size); 1577 try sc_cipher.writePacket(io, allocator, writer, reply.items); 1578 1579 return .{ 1580 .local_id = local_id, 1581 .remote_id = remote_id, 1582 .local_window = initial_recv_window, 1583 .remote_window = initial_window, 1584 .max_packet = max_packet, 1585 }; 1586 } 1587 1588 fn sendChannelOpenFailure( 1589 io: std.Io, 1590 allocator: std.mem.Allocator, 1591 sc_cipher: *Cipher, 1592 writer: *std.Io.Writer, 1593 open_packet: []const u8, 1594 reason: u32, 1595 description: []const u8, 1596 ) !void { 1597 // recover the sender_channel field from the CHANNEL_OPEN packet so we 1598 // address the failure to the correct id 1599 var r = std.Io.Reader.fixed(open_packet[1..]); 1600 const ch_type_len = try r.takeInt(u32, .big); 1601 try r.discardAll(ch_type_len); 1602 const remote_id = try r.takeInt(u32, .big); 1603 1604 var reply: std.ArrayList(u8) = .empty; 1605 defer reply.deinit(allocator); 1606 try reply.append(allocator, SSH_MSG_CHANNEL_OPEN_FAILURE); 1607 try writeU32(&reply, allocator, remote_id); 1608 try writeU32(&reply, allocator, reason); 1609 try writeStringField(&reply, allocator, description); 1610 try writeStringField(&reply, allocator, ""); // language tag 1611 try sc_cipher.writePacket(io, allocator, writer, reply.items); 1612 } 1613 1614 const RequestKind = enum { shell, exec }; 1615 1616 /// Returns the kind of session request if this triggered one, else null. 1617 fn handleChannelRequest( 1618 io: std.Io, 1619 allocator: std.mem.Allocator, 1620 sc_cipher: *Cipher, 1621 writer: *std.Io.Writer, 1622 ch: *Channel, 1623 packet: []const u8, 1624 pending_exec: *?[]u8, 1625 ) !?RequestKind { 1626 // byte SSH_MSG_CHANNEL_REQUEST 1627 // uint32 recipient_channel (== our local_id) 1628 // string request_type 1629 // boolean want_reply 1630 // …request-specific data 1631 var r = std.Io.Reader.fixed(packet[1..]); 1632 const recipient_channel = try r.takeInt(u32, .big); 1633 if (recipient_channel != ch.local_id) return error.UnknownChannel; 1634 const req_type = try takeStringField(allocator, &r, 64); 1635 defer allocator.free(req_type); 1636 const want_reply = (try r.takeByte()) != 0; 1637 1638 if (std.mem.eql(u8, req_type, "pty-req")) { 1639 // string TERM, uint32 width chars, uint32 height rows, uint32 width px, 1640 // uint32 height px, string modes 1641 const term = try takeStringField(allocator, &r, 64); 1642 defer allocator.free(term); 1643 const width = try r.takeInt(u32, .big); 1644 const height = try r.takeInt(u32, .big); 1645 ch.pty = .{ .width_cells = @intCast(@min(width, 0xFFFF)), .height_cells = @intCast(@min(height, 0xFFFF)) }; 1646 try replyChannelRequest(io, allocator, sc_cipher, writer, ch, want_reply, true); 1647 return null; 1648 } 1649 1650 if (std.mem.eql(u8, req_type, "env")) { 1651 // ignore environment variables silently (we don't pass them anywhere) 1652 try replyChannelRequest(io, allocator, sc_cipher, writer, ch, want_reply, true); 1653 return null; 1654 } 1655 1656 if (std.mem.eql(u8, req_type, "window-change")) { 1657 // uint32 width cells, uint32 height rows, uint32 width px, uint32 height px 1658 const width = try r.takeInt(u32, .big); 1659 const height = try r.takeInt(u32, .big); 1660 if (ch.pty != null) { 1661 ch.pty = .{ .width_cells = @intCast(@min(width, 0xFFFF)), .height_cells = @intCast(@min(height, 0xFFFF)) }; 1662 } 1663 // spec: window-change MUST NOT request a reply, but accept either 1664 try replyChannelRequest(io, allocator, sc_cipher, writer, ch, want_reply, true); 1665 return null; 1666 } 1667 1668 if (std.mem.eql(u8, req_type, "shell")) { 1669 try replyChannelRequest(io, allocator, sc_cipher, writer, ch, want_reply, true); 1670 return .shell; 1671 } 1672 1673 if (std.mem.eql(u8, req_type, "exec")) { 1674 // capture the command string for the handler to inspect 1675 const cmd = try takeStringField(allocator, &r, 4096); 1676 if (pending_exec.*) |old| allocator.free(old); 1677 pending_exec.* = cmd; 1678 try replyChannelRequest(io, allocator, sc_cipher, writer, ch, want_reply, true); 1679 return .exec; 1680 } 1681 1682 // unknown request kind — fail it 1683 try replyChannelRequest(io, allocator, sc_cipher, writer, ch, want_reply, false); 1684 return null; 1685 } 1686 1687 fn replyChannelRequest( 1688 io: std.Io, 1689 allocator: std.mem.Allocator, 1690 sc_cipher: *Cipher, 1691 writer: *std.Io.Writer, 1692 ch: *Channel, 1693 want_reply: bool, 1694 success: bool, 1695 ) !void { 1696 if (!want_reply) return; 1697 var reply: std.ArrayList(u8) = .empty; 1698 defer reply.deinit(allocator); 1699 try reply.append(allocator, if (success) SSH_MSG_CHANNEL_SUCCESS else SSH_MSG_CHANNEL_FAILURE); 1700 try writeU32(&reply, allocator, ch.remote_id); 1701 try sc_cipher.writePacket(io, allocator, writer, reply.items); 1702 } 1703 1704 fn handleWindowAdjust(ch: *Channel, packet: []const u8) !void { 1705 // byte SSH_MSG_CHANNEL_WINDOW_ADJUST 1706 // uint32 recipient_channel 1707 // uint32 bytes_to_add 1708 var r = std.Io.Reader.fixed(packet[1..]); 1709 const recipient_channel = try r.takeInt(u32, .big); 1710 if (recipient_channel != ch.local_id) return error.UnknownChannel; 1711 const add = try r.takeInt(u32, .big); 1712 ch.remote_window +|= add; 1713 } 1714 1715 fn discardChannelData( 1716 io: std.Io, 1717 allocator: std.mem.Allocator, 1718 sc_cipher: *Cipher, 1719 writer: *std.Io.Writer, 1720 ch: *Channel, 1721 packet: []const u8, 1722 extended: bool, 1723 ) !void { 1724 // byte SSH_MSG_CHANNEL_DATA/EXTENDED_DATA 1725 // uint32 recipient_channel 1726 // [if extended: uint32 type_code] 1727 // string data 1728 const payload = try parseChannelData(packet, extended, ch.local_id); 1729 const data_len: u32 = @intCast(payload.len); 1730 1731 if (data_len > ch.local_window) return error.WindowExceeded; 1732 ch.local_window -= data_len; 1733 try maybeRefillRecvWindow(io, allocator, sc_cipher, writer, ch); 1734 } 1735 1736 pub fn parseChannelData(packet: []const u8, extended: bool, expected_channel: u32) ![]const u8 { 1737 if (packet.len < 1) return error.EmptyPacket; 1738 var r = std.Io.Reader.fixed(packet[1..]); 1739 const recipient_channel = try r.takeInt(u32, .big); 1740 if (recipient_channel != expected_channel) return error.UnknownChannel; 1741 if (extended) { 1742 const data_type = try r.takeInt(u32, .big); 1743 if (data_type != SSH_EXTENDED_DATA_STDERR) return error.UnsupportedExtendedData; 1744 } 1745 const data_len = try r.takeInt(u32, .big); 1746 // catches both data_len > remaining (would otherwise be a bounds-check 1747 // panic on the slice) and trailing junk after the declared payload. 1748 if (data_len != r.buffered().len) return error.InvalidChannelData; 1749 return r.buffered(); 1750 } 1751 1752 fn parseChannelId(packet: []const u8, expected_channel: u32) !void { 1753 if (packet.len < 1) return error.EmptyPacket; 1754 var r = std.Io.Reader.fixed(packet[1..]); 1755 const recipient_channel = try r.takeInt(u32, .big); 1756 if (recipient_channel != expected_channel) return error.UnknownChannel; 1757 if (r.buffered().len != 0) return error.InvalidChannelMessage; 1758 } 1759 1760 /// Send a CHANNEL_WINDOW_ADJUST if our receive window has dipped below half 1761 /// of the initial value. Used both for the pre-session discard path and by 1762 /// SessionCtx after it consumes a CHANNEL_DATA payload. 1763 fn maybeRefillRecvWindow( 1764 io: std.Io, 1765 allocator: std.mem.Allocator, 1766 sc_cipher: *Cipher, 1767 writer: *std.Io.Writer, 1768 ch: *Channel, 1769 ) !void { 1770 if (ch.local_window >= initial_recv_window / 2) return; 1771 const add = initial_recv_window - ch.local_window; 1772 ch.local_window += add; 1773 var adj: std.ArrayList(u8) = .empty; 1774 defer adj.deinit(allocator); 1775 try adj.append(allocator, SSH_MSG_CHANNEL_WINDOW_ADJUST); 1776 try writeU32(&adj, allocator, ch.remote_id); 1777 try writeU32(&adj, allocator, add); 1778 try sc_cipher.writePacket(io, allocator, writer, adj.items); 1779 } 1780 1781 pub fn writeU32(buf: *std.ArrayList(u8), allocator: std.mem.Allocator, value: u32) !void { 1782 var bytes: [4]u8 = undefined; 1783 std.mem.writeInt(u32, &bytes, value, .big); 1784 try buf.appendSlice(allocator, &bytes); 1785 } 1786