╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_ssh_protocol.zig ───────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1476 }, 1477 1478 SSH_MSG_CHANNEL_REQUEST => { 1479 const ch = if (channel) |*c| c else continue; 1480 const start_request = try handleChannelRequest(io, allocator, sc_cipher, writer, ch, packet, &pending_exec); 1481 if (start_request) |req_kind| { 1482 const request: Request = switch (req_kind) { 1483 .shell => .{ .shell = ch.pty }, 1484 .exec => .{ .exec = pending_exec.? }, 1485 }; 1486 var sess = SessionCtx{ 1487 .io = io, 1488 .allocator = allocator, 1489 .reader = reader, 1490 .writer = writer, 1491 .cs_cipher = cs_cipher, 1492 .sc_cipher = sc_cipher, 1493 .channel = ch, 1494 .fingerprint = fingerprint.*, 1495 }; 1496 defer sess.deinit(); 1497 handler.handleSession(&sess, request) catch |session_err| { 1498 // try to send a non-zero exit status before closing 1499 sess.exit(1) catch {}; 1500 return session_err; 1501 }; 1502 if (!sess.closed) try sess.exit(0); 1503 return; 1504 } 1505 }, 1506 1507 SSH_MSG_CHANNEL_WINDOW_ADJUST => { 1508 const ch = if (channel) |*c| c else continue; 1509 try handleWindowAdjust(ch, packet); 1510 }, 1511 1512 SSH_MSG_CHANNEL_DATA, SSH_MSG_CHANNEL_EXTENDED_DATA => { 1513 const ch = if (channel) |*c| c else continue; 1514 // before shell/exec the client shouldn't be sending data, but 1515 // some clients are chatty; discard and refill window. 1516 try discardChannelData(io, allocator, sc_cipher, writer, ch, packet, msg_type == SSH_MSG_CHANNEL_EXTENDED_DATA); 1517 }, 1518 1519 SSH_MSG_CHANNEL_EOF => { 1520 if (channel) |*c| { 1521 try parseChannelId(packet, c.local_id); 1522 c.eof_received = true; 1523 } 1524 }, 1525 1526 SSH_MSG_CHANNEL_CLOSE => { 1527 if (channel) |*c| try parseChannelId(packet, c.local_id); 1528 return; 1529 }, 1530 1531 else => {}, // ignore unknown messages 1532 } 1533 } 1534 } 1535 1536 fn handleGlobalRequest( 1537 io: std.Io, 1538 allocator: std.mem.Allocator, 1539 sc_cipher: *Cipher, 1540 writer: *std.Io.Writer, 1541 packet: []const u8, 1542 ) !void { 1543 // byte SSH_MSG_GLOBAL_REQUEST 1544 // string request_name 1545 // boolean want_reply 1546 // …request-specific data we ignore 1547 var r = std.Io.Reader.fixed(packet[1..]); 1548 const name = try takeStringField(allocator, &r, 256); 1549 defer allocator.free(name); 1550 const want_reply = (try r.takeByte()) != 0; 1551 if (want_reply) { 1552 try sc_cipher.writePacket(io, allocator, writer, &[_]u8{SSH_MSG_REQUEST_FAILURE}); 1553 } 1554 } 1555 1556 /// Parse a CHANNEL_OPEN. On a session channel, send CHANNEL_OPEN_CONFIRMATION 1557 /// and return the new Channel. On other types, send CHANNEL_OPEN_FAILURE and 1558 /// return null. 1559 fn handleChannelOpen( 1560 io: std.Io, 1561 allocator: std.mem.Allocator, 1562 sc_cipher: *Cipher, 1563 writer: *std.Io.Writer, 1564 packet: []const u8, 1565 ) !?Channel { 1566 // byte SSH_MSG_CHANNEL_OPEN 1567 // string channel_type 1568 // uint32 sender_channel (peer's id) 1569 // uint32 initial_window 1570 // uint32 max_packet 1571 var r = std.Io.Reader.fixed(packet[1..]); 1572 const ch_type = try takeStringField(allocator, &r, 64); 1573 defer allocator.free(ch_type); 1574 const remote_id = try r.takeInt(u32, .big); 1575 const initial_window = try r.takeInt(u32, .big); 1576 const max_packet = try r.takeInt(u32, .big); 1577 1578 if (!std.mem.eql(u8, ch_type, "session")) { 1579 try sendChannelOpenFailure(io, allocator, sc_cipher, writer, packet, SSH_OPEN_UNKNOWN_CHANNEL_TYPE, "only session channels are supported"); 1580 return null; 1581 } 1582 1583 const local_id: u32 = 0; // single-channel connection — id is always 0 from our side 1584 var reply: std.ArrayList(u8) = .empty; 1585 defer reply.deinit(allocator); 1586 try reply.append(allocator, SSH_MSG_CHANNEL_OPEN_CONFIRMATION); 1587 try writeU32(&reply, allocator, remote_id); 1588 try writeU32(&reply, allocator, local_id); 1589 try writeU32(&reply, allocator, initial_recv_window); 1590 try writeU32(&reply, allocator, max_packet_size); 1591 try sc_cipher.writePacket(io, allocator, writer, reply.items); 1592 1593 return .{ 1594 .local_id = local_id, 1595 .remote_id = remote_id, 1596 .local_window = initial_recv_window, 1597 .remote_window = initial_window, 1598 .max_packet = max_packet, 1599 }; 1600 } 1601 1602 fn sendChannelOpenFailure( 1603 io: std.Io, 1604 allocator: std.mem.Allocator, 1605 sc_cipher: *Cipher, 1606 writer: *std.Io.Writer, 1607 open_packet: []const u8, 1608 reason: u32, 1609 description: []const u8, 1610 ) !void { 1611 // recover the sender_channel field from the CHANNEL_OPEN packet so we 1612 // address the failure to the correct id 1613 var r = std.Io.Reader.fixed(open_packet[1..]); 1614 const ch_type_len = try r.takeInt(u32, .big); 1615 try r.discardAll(ch_type_len); 1616 const remote_id = try r.takeInt(u32, .big); 1617 1618 var reply: std.ArrayList(u8) = .empty; 1619 defer reply.deinit(allocator); 1620 try reply.append(allocator, SSH_MSG_CHANNEL_OPEN_FAILURE); 1621 try writeU32(&reply, allocator, remote_id); 1622 try writeU32(&reply, allocator, reason); 1623 try writeStringField(&reply, allocator, description); 1624 try writeStringField(&reply, allocator, ""); // language tag 1625 try sc_cipher.writePacket(io, allocator, writer, reply.items); 1626 } 1627 1628 const RequestKind = enum { shell, exec }; 1629 1630 /// Returns the kind of session request if this triggered one, else null. 1631 fn handleChannelRequest( 1632 io: std.Io, 1633 allocator: std.mem.Allocator, 1634 sc_cipher: *Cipher, 1635 writer: *std.Io.Writer, 1636 ch: *Channel, 1637 packet: []const u8, 1638 pending_exec: *?[]u8, 1639 ) !?RequestKind { 1640 // byte SSH_MSG_CHANNEL_REQUEST 1641 // uint32 recipient_channel (== our local_id) 1642 // string request_type 1643 // boolean want_reply 1644 // …request-specific data 1645 var r = std.Io.Reader.fixed(packet[1..]); 1646 const recipient_channel = try r.takeInt(u32, .big); 1647 if (recipient_channel != ch.local_id) return error.UnknownChannel; 1648 const req_type = try takeStringField(allocator, &r, 64); 1649 defer allocator.free(req_type); 1650 const want_reply = (try r.takeByte()) != 0; 1651 1652 if (std.mem.eql(u8, req_type, "pty-req")) { 1653 // string TERM, uint32 width chars, uint32 height rows, uint32 width px, 1654 // uint32 height px, string modes 1655 const term = try takeStringField(allocator, &r, 64); 1656 defer allocator.free(term); 1657 const width = try r.takeInt(u32, .big); 1658 const height = try r.takeInt(u32, .big); 1659 ch.pty = .{ .width_cells = @intCast(@min(width, 0xFFFF)), .height_cells = @intCast(@min(height, 0xFFFF)) }; 1660 try replyChannelRequest(io, allocator, sc_cipher, writer, ch, want_reply, true); 1661 return null; 1662 } 1663 1664 if (std.mem.eql(u8, req_type, "env")) { 1665 // ignore environment variables silently (we don't pass them anywhere) 1666 try replyChannelRequest(io, allocator, sc_cipher, writer, ch, want_reply, true); 1667 return null; 1668 } 1669 1670 if (std.mem.eql(u8, req_type, "window-change")) { 1671 // uint32 width cells, uint32 height rows, uint32 width px, uint32 height px 1672 const width = try r.takeInt(u32, .big); 1673 const height = try r.takeInt(u32, .big); 1674 if (ch.pty != null) { 1675 ch.pty = .{ .width_cells = @intCast(@min(width, 0xFFFF)), .height_cells = @intCast(@min(height, 0xFFFF)) }; 1676 } 1677 // spec: window-change MUST NOT request a reply, but accept either 1678 try replyChannelRequest(io, allocator, sc_cipher, writer, ch, want_reply, true); 1679 return null; 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