╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_ssh_protocol.zig ───────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1368 fn runChannelLayer( 1369 io: std.Io, 1370 allocator: std.mem.Allocator, 1371 reader: *std.Io.Reader, 1372 writer: *std.Io.Writer, 1373 cs_cipher: *Cipher, 1374 sc_cipher: *Cipher, 1375 fingerprint: *const [fingerprint_len]u8, 1376 handler: anytype, 1377 ) !void { 1378 var channel: ?Channel = null; 1379 var pending_exec: ?[]u8 = null; 1380 defer if (pending_exec) |s| allocator.free(s); 1381 1382 while (true) { 1383 const packet = try cs_cipher.readPacket(allocator, reader); 1384 defer allocator.free(packet); 1385 if (packet.len < 1) return error.EmptyPacket; 1386 const msg_type = packet[0]; 1387 1388 switch (msg_type) { 1389 SSH_MSG_GLOBAL_REQUEST => try handleGlobalRequest(io, allocator, sc_cipher, writer, packet), 1390 1391 SSH_MSG_KEXINIT => { 1392 try sendDisconnect(io, allocator, sc_cipher, writer, "rekey is not supported"); 1393 return error.RekeyUnsupported; 1394 }, 1395 1396 SSH_MSG_CHANNEL_OPEN => { 1397 if (channel != null) { 1398 try sendChannelOpenFailure(io, allocator, sc_cipher, writer, packet, SSH_OPEN_RESOURCE_SHORTAGE, "only one channel per connection"); 1399 continue; 1400 } 1401 channel = try handleChannelOpen(io, allocator, sc_cipher, writer, packet) orelse continue; 1402 }, 1403 1404 SSH_MSG_CHANNEL_REQUEST => { 1405 const ch = if (channel) |*c| c else continue; 1406 const start_request = try handleChannelRequest(io, allocator, sc_cipher, writer, ch, packet, &pending_exec); 1407 if (start_request) |req_kind| { 1408 const request: Request = switch (req_kind) { 1409 .shell => .{ .shell = ch.pty }, 1410 .exec => .{ .exec = pending_exec.? }, 1411 }; 1412 var sess = SessionCtx{ 1413 .io = io, 1414 .allocator = allocator, 1415 .reader = reader, 1416 .writer = writer, 1417 .cs_cipher = cs_cipher, 1418 .sc_cipher = sc_cipher, 1419 .channel = ch, 1420 .fingerprint = fingerprint.*, 1421 }; 1422 defer sess.deinit(); 1423 handler.handleSession(&sess, request) catch |session_err| { 1424 // try to send a non-zero exit status before closing 1425 sess.exit(1) catch {}; 1426 return session_err; 1427 }; 1428 if (!sess.closed) try sess.exit(0); 1429 return; 1430 } 1431 }, 1432 1433 SSH_MSG_CHANNEL_WINDOW_ADJUST => { 1434 const ch = if (channel) |*c| c else continue; 1435 try handleWindowAdjust(ch, packet); 1436 }, 1437 1438 SSH_MSG_CHANNEL_DATA, SSH_MSG_CHANNEL_EXTENDED_DATA => { 1439 const ch = if (channel) |*c| c else continue; 1440 // before shell/exec the client shouldn't be sending data, but 1441 // some clients are chatty; discard and refill window. 1442 try discardChannelData(io, allocator, sc_cipher, writer, ch, packet, msg_type == SSH_MSG_CHANNEL_EXTENDED_DATA); 1443 }, 1444 1445 SSH_MSG_CHANNEL_EOF => { 1446 if (channel) |*c| { 1447 try parseChannelId(packet, c.local_id); 1448 c.eof_received = true; 1449 } 1450 }, 1451 1452 SSH_MSG_CHANNEL_CLOSE => { 1453 if (channel) |*c| try parseChannelId(packet, c.local_id); 1454 return; 1455 }, 1456 1457 else => {}, // ignore unknown messages 1458 } 1459 } 1460 } 1461 1462 fn handleGlobalRequest( 1463 io: std.Io, 1464 allocator: std.mem.Allocator, 1465 sc_cipher: *Cipher, 1466 writer: *std.Io.Writer, 1467 packet: []const u8, 1468 ) !void { 1469 // byte SSH_MSG_GLOBAL_REQUEST 1470 // string request_name 1471 // boolean want_reply 1472 // …request-specific data we ignore 1473 var r = std.Io.Reader.fixed(packet[1..]); 1474 const name = try takeStringField(allocator, &r, 256); 1475 defer allocator.free(name); 1476 const want_reply = (try r.takeByte()) != 0; 1477 if (want_reply) { 1478 try sc_cipher.writePacket(io, allocator, writer, &[_]u8{SSH_MSG_REQUEST_FAILURE}); 1479 } 1480 } 1481 1482 /// Parse a CHANNEL_OPEN. On a session channel, send CHANNEL_OPEN_CONFIRMATION 1483 /// and return the new Channel. On other types, send CHANNEL_OPEN_FAILURE and 1484 /// return null. 1485 fn handleChannelOpen( 1486 io: std.Io, 1487 allocator: std.mem.Allocator, 1488 sc_cipher: *Cipher, 1489 writer: *std.Io.Writer, 1490 packet: []const u8, 1491 ) !?Channel { 1492 // byte SSH_MSG_CHANNEL_OPEN 1493 // string channel_type 1494 // uint32 sender_channel (peer's id) 1495 // uint32 initial_window 1496 // uint32 max_packet 1497 var r = std.Io.Reader.fixed(packet[1..]); 1498 const ch_type = try takeStringField(allocator, &r, 64); 1499 defer allocator.free(ch_type); 1500 const remote_id = try r.takeInt(u32, .big); 1501 const initial_window = try r.takeInt(u32, .big); 1502 const max_packet = try r.takeInt(u32, .big); 1503 1504 if (!std.mem.eql(u8, ch_type, "session")) { 1505 try sendChannelOpenFailure(io, allocator, sc_cipher, writer, packet, SSH_OPEN_UNKNOWN_CHANNEL_TYPE, "only session channels are supported"); 1506 return null; 1507 } 1508 1509 const local_id: u32 = 0; // single-channel connection — id is always 0 from our side 1510 var reply: std.ArrayList(u8) = .empty; 1511 defer reply.deinit(allocator); 1512 try reply.append(allocator, SSH_MSG_CHANNEL_OPEN_CONFIRMATION); 1513 try writeU32(&reply, allocator, remote_id); 1514 try writeU32(&reply, allocator, local_id); 1515 try writeU32(&reply, allocator, initial_recv_window); 1516 try writeU32(&reply, allocator, max_packet_size); 1517 try sc_cipher.writePacket(io, allocator, writer, reply.items); 1518 1519 return .{ 1520 .local_id = local_id, 1521 .remote_id = remote_id, 1522 .local_window = initial_recv_window, 1523 .remote_window = initial_window, 1524 .max_packet = max_packet, 1525 }; 1526 } 1527 1528 fn sendChannelOpenFailure( 1529 io: std.Io, 1530 allocator: std.mem.Allocator, 1531 sc_cipher: *Cipher, 1532 writer: *std.Io.Writer, 1533 open_packet: []const u8, 1534 reason: u32, 1535 description: []const u8, 1536 ) !void { 1537 // recover the sender_channel field from the CHANNEL_OPEN packet so we 1538 // address the failure to the correct id 1539 var r = std.Io.Reader.fixed(open_packet[1..]); 1540 const ch_type_len = try r.takeInt(u32, .big); 1541 try r.discardAll(ch_type_len); 1542 const remote_id = try r.takeInt(u32, .big); 1543 1544 var reply: std.ArrayList(u8) = .empty; 1545 defer reply.deinit(allocator); 1546 try reply.append(allocator, SSH_MSG_CHANNEL_OPEN_FAILURE); 1547 try writeU32(&reply, allocator, remote_id); 1548 try writeU32(&reply, allocator, reason); 1549 try writeStringField(&reply, allocator, description); 1550 try writeStringField(&reply, allocator, ""); // language tag 1551 try sc_cipher.writePacket(io, allocator, writer, reply.items); 1552 } 1553 1554 const RequestKind = enum { shell, exec }; 1555 1556 /// Returns the kind of session request if this triggered one, else null. 1557 fn handleChannelRequest( 1558 io: std.Io, 1559 allocator: std.mem.Allocator, 1560 sc_cipher: *Cipher, 1561 writer: *std.Io.Writer, 1562 ch: *Channel, 1563 packet: []const u8, 1564 pending_exec: *?[]u8, 1565 ) !?RequestKind { 1566 // byte SSH_MSG_CHANNEL_REQUEST 1567 // uint32 recipient_channel (== our local_id) 1568 // string request_type 1569 // boolean want_reply 1570 // …request-specific data 1571 var r = std.Io.Reader.fixed(packet[1..]); 1572 const recipient_channel = try r.takeInt(u32, .big); 1573 if (recipient_channel != ch.local_id) return error.UnknownChannel; 1574 const req_type = try takeStringField(allocator, &r, 64); 1575 defer allocator.free(req_type); 1576 const want_reply = (try r.takeByte()) != 0; 1577 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