╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_ssh_protocol.zig ───────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1481 }, 1482 1483 SSH_MSG_CHANNEL_REQUEST => { 1484 const ch = if (channel) |*c| c else continue; 1485 const start_request = try handleChannelRequest(io, allocator, sc_cipher, writer, ch, packet, &pending_exec); 1486 if (start_request) |req_kind| { 1487 const request: Request = switch (req_kind) { 1488 .shell => .{ .shell = ch.pty }, 1489 .exec => .{ .exec = pending_exec.? }, 1490 }; 1491 var sess = SessionCtx{ 1492 .io = io, 1493 .allocator = allocator, 1494 .reader = reader, 1495 .writer = writer, 1496 .cs_cipher = cs_cipher, 1497 .sc_cipher = sc_cipher, 1498 .channel = ch, 1499 .fingerprint = fingerprint.*, 1500 }; 1501 defer sess.deinit(); 1502 handler.handleSession(&sess, request) catch |session_err| { 1503 // try to send a non-zero exit status before closing 1504 sess.exit(1) catch {}; 1505 return session_err; 1506 }; 1507 if (!sess.closed) try sess.exit(0); 1508 return; 1509 } 1510 }, 1511 1512 SSH_MSG_CHANNEL_WINDOW_ADJUST => { 1513 const ch = if (channel) |*c| c else continue; 1514 try handleWindowAdjust(ch, packet); 1515 }, 1516 1517 SSH_MSG_CHANNEL_DATA, SSH_MSG_CHANNEL_EXTENDED_DATA => { 1518 const ch = if (channel) |*c| c else continue; 1519 // before shell/exec the client shouldn't be sending data, but 1520 // some clients are chatty; discard and refill window. 1521 try discardChannelData(io, allocator, sc_cipher, writer, ch, packet, msg_type == SSH_MSG_CHANNEL_EXTENDED_DATA); 1522 }, 1523 1524 SSH_MSG_CHANNEL_EOF => { 1525 if (channel) |*c| { 1526 try parseChannelId(packet, c.local_id); 1527 c.eof_received = true; 1528 } 1529 }, 1530 1531 SSH_MSG_CHANNEL_CLOSE => { 1532 if (channel) |*c| try parseChannelId(packet, c.local_id); 1533 return; 1534 }, 1535 1536 else => {}, // ignore unknown messages 1537 } 1538 } 1539 } 1540 1541 fn handleGlobalRequest( 1542 io: std.Io, 1543 allocator: std.mem.Allocator, 1544 sc_cipher: *Cipher, 1545 writer: *std.Io.Writer, 1546 packet: []const u8, 1547 ) !void { 1548 // byte SSH_MSG_GLOBAL_REQUEST 1549 // string request_name 1550 // boolean want_reply 1551 // …request-specific data we ignore 1552 var r = std.Io.Reader.fixed(packet[1..]); 1553 const name = try takeStringField(allocator, &r, 256); 1554 defer allocator.free(name); 1555 const want_reply = (try r.takeByte()) != 0; 1556 if (want_reply) { 1557 try sc_cipher.writePacket(io, allocator, writer, &[_]u8{SSH_MSG_REQUEST_FAILURE}); 1558 } 1559 } 1560 1561 /// Parse a CHANNEL_OPEN. On a session channel, send CHANNEL_OPEN_CONFIRMATION 1562 /// and return the new Channel. On other types, send CHANNEL_OPEN_FAILURE and 1563 /// return null. 1564 fn handleChannelOpen( 1565 io: std.Io, 1566 allocator: std.mem.Allocator, 1567 sc_cipher: *Cipher, 1568 writer: *std.Io.Writer, 1569 packet: []const u8, 1570 ) !?Channel { 1571 // byte SSH_MSG_CHANNEL_OPEN 1572 // string channel_type 1573 // uint32 sender_channel (peer's id) 1574 // uint32 initial_window 1575 // uint32 max_packet 1576 var r = std.Io.Reader.fixed(packet[1..]); 1577 const ch_type = try takeStringField(allocator, &r, 64); 1578 defer allocator.free(ch_type); 1579 const remote_id = try r.takeInt(u32, .big); 1580 const initial_window = try r.takeInt(u32, .big); 1581 const max_packet = try r.takeInt(u32, .big); 1582 1583 if (!std.mem.eql(u8, ch_type, "session")) { 1584 try sendChannelOpenFailure(io, allocator, sc_cipher, writer, packet, SSH_OPEN_UNKNOWN_CHANNEL_TYPE, "only session channels are supported"); 1585 return null; 1586 } 1587 1588 const local_id: u32 = 0; // single-channel connection — id is always 0 from our side 1589 var reply: std.ArrayList(u8) = .empty; 1590 defer reply.deinit(allocator); 1591 try reply.append(allocator, SSH_MSG_CHANNEL_OPEN_CONFIRMATION); 1592 try writeU32(&reply, allocator, remote_id); 1593 try writeU32(&reply, allocator, local_id); 1594 try writeU32(&reply, allocator, initial_recv_window); 1595 try writeU32(&reply, allocator, max_packet_size); 1596 try sc_cipher.writePacket(io, allocator, writer, reply.items); 1597 1598 return .{ 1599 .local_id = local_id, 1600 .remote_id = remote_id, 1601 .local_window = initial_recv_window, 1602 .remote_window = initial_window, 1603 .max_packet = max_packet, 1604 }; 1605 } 1606 1607 fn sendChannelOpenFailure( 1608 io: std.Io, 1609 allocator: std.mem.Allocator, 1610 sc_cipher: *Cipher, 1611 writer: *std.Io.Writer, 1612 open_packet: []const u8, 1613 reason: u32, 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