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