╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_ssh_protocol.zig ───────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1578 if (std.mem.eql(u8, req_type, "pty-req")) { 1579 // string TERM, uint32 width chars, uint32 height rows, uint32 width px, 1580 // uint32 height px, string modes 1581 const term = try takeStringField(allocator, &r, 64); 1582 defer allocator.free(term); 1583 const width = try r.takeInt(u32, .big); 1584 const height = try r.takeInt(u32, .big); 1585 ch.pty = .{ .width_cells = @intCast(@min(width, 0xFFFF)), .height_cells = @intCast(@min(height, 0xFFFF)) }; 1586 try replyChannelRequest(io, allocator, sc_cipher, writer, ch, want_reply, true); 1587 return null; 1588 } 1589 1590 if (std.mem.eql(u8, req_type, "env")) { 1591 // ignore environment variables silently (we don't pass them anywhere) 1592 try replyChannelRequest(io, allocator, sc_cipher, writer, ch, want_reply, true); 1593 return null; 1594 } 1595 1596 if (std.mem.eql(u8, req_type, "window-change")) { 1597 // uint32 width cells, uint32 height rows, uint32 width px, uint32 height px 1598 const width = try r.takeInt(u32, .big); 1599 const height = try r.takeInt(u32, .big); 1600 if (ch.pty != null) { 1601 ch.pty = .{ .width_cells = @intCast(@min(width, 0xFFFF)), .height_cells = @intCast(@min(height, 0xFFFF)) }; 1602 } 1603 // spec: window-change MUST NOT request a reply, but accept either 1604 try replyChannelRequest(io, allocator, sc_cipher, writer, ch, want_reply, true); 1605 return null; 1606 } 1607 1608 if (std.mem.eql(u8, req_type, "shell")) { 1609 try replyChannelRequest(io, allocator, sc_cipher, writer, ch, want_reply, true); 1610 return .shell; 1611 } 1612 1613 if (std.mem.eql(u8, req_type, "exec")) { 1614 // capture the command string for the handler to inspect 1615 const cmd = try takeStringField(allocator, &r, 4096); 1616 if (pending_exec.*) |old| allocator.free(old); 1617 pending_exec.* = cmd; 1618 try replyChannelRequest(io, allocator, sc_cipher, writer, ch, want_reply, true); 1619 return .exec; 1620 } 1621 1622 // unknown request kind — fail it 1623 try replyChannelRequest(io, allocator, sc_cipher, writer, ch, want_reply, false); 1624 return null; 1625 } 1626 1627 fn replyChannelRequest( 1628 io: std.Io, 1629 allocator: std.mem.Allocator, 1630 sc_cipher: *Cipher, 1631 writer: *std.Io.Writer, 1632 ch: *Channel, 1633 want_reply: bool, 1634 success: bool, 1635 ) !void { 1636 if (!want_reply) return; 1637 var reply: std.ArrayList(u8) = .empty; 1638 defer reply.deinit(allocator); 1639 try reply.append(allocator, if (success) SSH_MSG_CHANNEL_SUCCESS else SSH_MSG_CHANNEL_FAILURE); 1640 try writeU32(&reply, allocator, ch.remote_id); 1641 try sc_cipher.writePacket(io, allocator, writer, reply.items); 1642 } 1643 1644 fn handleWindowAdjust(ch: *Channel, packet: []const u8) !void { 1645 // byte SSH_MSG_CHANNEL_WINDOW_ADJUST 1646 // uint32 recipient_channel 1647 // uint32 bytes_to_add 1648 var r = std.Io.Reader.fixed(packet[1..]); 1649 const recipient_channel = try r.takeInt(u32, .big); 1650 if (recipient_channel != ch.local_id) return error.UnknownChannel; 1651 const add = try r.takeInt(u32, .big); 1652 ch.remote_window +|= add; 1653 } 1654 1655 fn discardChannelData( 1656 io: std.Io, 1657 allocator: std.mem.Allocator, 1658 sc_cipher: *Cipher, 1659 writer: *std.Io.Writer, 1660 ch: *Channel, 1661 packet: []const u8, 1662 extended: bool, 1663 ) !void { 1664 // byte SSH_MSG_CHANNEL_DATA/EXTENDED_DATA 1665 // uint32 recipient_channel 1666 // [if extended: uint32 type_code] 1667 // string data 1668 const payload = try parseChannelData(packet, extended, ch.local_id); 1669 const data_len: u32 = @intCast(payload.len); 1670 1671 if (data_len > ch.local_window) return error.WindowExceeded; 1672 ch.local_window -= data_len; 1673 try maybeRefillRecvWindow(io, allocator, sc_cipher, writer, ch); 1674 } 1675 1676 pub fn parseChannelData(packet: []const u8, extended: bool, expected_channel: u32) ![]const u8 { 1677 if (packet.len < 1) return error.EmptyPacket; 1678 var r = std.Io.Reader.fixed(packet[1..]); 1679 const recipient_channel = try r.takeInt(u32, .big); 1680 if (recipient_channel != expected_channel) return error.UnknownChannel; 1681 if (extended) { 1682 const data_type = try r.takeInt(u32, .big); 1683 if (data_type != SSH_EXTENDED_DATA_STDERR) return error.UnsupportedExtendedData; 1684 } 1685 const data_len = try r.takeInt(u32, .big); 1686 // catches both data_len > remaining (would otherwise be a bounds-check 1687 // panic on the slice) and trailing junk after the declared payload. 1688 if (data_len != r.buffered().len) return error.InvalidChannelData; 1689 return r.buffered(); 1690 } 1691 1692 fn parseChannelId(packet: []const u8, expected_channel: u32) !void { 1693 if (packet.len < 1) return error.EmptyPacket; 1694 var r = std.Io.Reader.fixed(packet[1..]); 1695 const recipient_channel = try r.takeInt(u32, .big); 1696 if (recipient_channel != expected_channel) return error.UnknownChannel; 1697 if (r.buffered().len != 0) return error.InvalidChannelMessage; 1698 } 1699 1700 /// Send a CHANNEL_WINDOW_ADJUST if our receive window has dipped below half 1701 /// of the initial value. Used both for the pre-session discard path and by 1702 /// SessionCtx after it consumes a CHANNEL_DATA payload. 1703 fn maybeRefillRecvWindow( 1704 io: std.Io, 1705 allocator: std.mem.Allocator, 1706 sc_cipher: *Cipher, 1707 writer: *std.Io.Writer, 1708 ch: *Channel, 1709 ) !void { 1710 if (ch.local_window >= initial_recv_window / 2) return; 1711 const add = initial_recv_window - ch.local_window; 1712 ch.local_window += add; 1713 var adj: std.ArrayList(u8) = .empty; 1714 defer adj.deinit(allocator); 1715 try adj.append(allocator, SSH_MSG_CHANNEL_WINDOW_ADJUST); 1716 try writeU32(&adj, allocator, ch.remote_id); 1717 try writeU32(&adj, allocator, add); 1718 try sc_cipher.writePacket(io, allocator, writer, adj.items); 1719 } 1720 1721 pub fn writeU32(buf: *std.ArrayList(u8), allocator: std.mem.Allocator, value: u32) !void { 1722 var bytes: [4]u8 = undefined; 1723 std.mem.writeInt(u32, &bytes, value, .big); 1724 try buf.appendSlice(allocator, &bytes); 1725 } 1726