╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ssh.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
742 var stage: [256]u8 = undefined; 743 const cap = limit.minInt(stage.len); 744 if (cap == 0) return 0; 745 const n = self.pipe.get(self.io, stage[0..cap], 1) catch |err| switch (err) { 746 error.Closed => return error.EndOfStream, 747 error.Canceled => return error.ReadFailed, 748 }; 749 return try w.write(stage[0..n]); 750 } 751 752 fn readVec(r: *std.Io.Reader, data: [][]u8) std.Io.Reader.Error!usize { 753 const self: *PipeReader = @fieldParentPtr("interface", r); 754 // vtable contract: if data[0] is empty, fill r.buffer[r.end..] instead. 755 const dest = if (data.len == 0 or data[0].len == 0) r.buffer[r.end..] else data[0]; 756 if (dest.len == 0) return 0; 757 const n = self.pipe.get(self.io, dest, 1) catch |err| switch (err) { 758 error.Closed => return error.EndOfStream, 759 error.Canceled => return error.ReadFailed, 760 }; 761 if (data.len == 0 or data[0].len == 0) { 762 r.end += n; 763 return 0; 764 } 765 return n; 766 } 767 }; 768 769 const PipeWriter = struct { 770 interface: std.Io.Writer, 771 io: std.Io, 772 pipe: *Pipe, 773 774 fn init(io: std.Io, pipe: *Pipe, buf: []u8) PipeWriter { 775 return .{ 776 .interface = .{ 777 .vtable = &.{ .drain = drain }, 778 .buffer = buf, 779 }, 780 .io = io, 781 .pipe = pipe, 782 }; 783 } 784 785 fn drain(w: *std.Io.Writer, data: []const []const u8, splat: usize) std.Io.Writer.Error!usize { 786 const self: *PipeWriter = @fieldParentPtr("interface", w); 787 const buffered = w.buffered(); 788 if (buffered.len > 0) self.pipe.putAll(self.io, buffered) catch return error.WriteFailed; 789 790 var extra: usize = 0; 791 if (data.len > 1) { 792 for (data[0 .. data.len - 1]) |chunk| { 793 if (chunk.len == 0) continue; 794 self.pipe.putAll(self.io, chunk) catch return error.WriteFailed; 795 extra += chunk.len; 796 } 797 } 798 if (data.len > 0) { 799 const pattern = data[data.len - 1]; 800 if (pattern.len > 0) { 801 var i: usize = 0; 802 while (i < splat) : (i += 1) { 803 self.pipe.putAll(self.io, pattern) catch return error.WriteFailed; 804 extra += pattern.len; 805 } 806 } 807 } 808 return w.consume(buffered.len + extra); 809 } 810 }; 811 812 // runs proto.handleConnection in a concurrent task (spawned via std.Io.concurrent). 813 // also acts as the session handler — captures the authenticated key's 814 // fingerprint and returns immediately, leaving runChannelLayer's wrap-up to 815 // send exit-status / EOF / CLOSE. records any error so the test can assert 816 // on it after awaiting the future. 817 const ServerTask = struct { 818 io: std.Io, 819 allocator: std.mem.Allocator, 820 host_key: *const proto.HostKey, 821 reader: *std.Io.Reader, 822 writer: *std.Io.Writer, 823 outgoing: *Pipe, // closed on exit so the client's reads return EOF 824 captured_fp: [proto.fingerprint_len]u8 = undefined, 825 captured_exec_buf: [256]u8 = undefined, 826 captured_exec_len: usize = 0, 827 result: ?anyerror = null, 828 829 pub fn handleSession(self: *ServerTask, sess: *proto.SessionCtx, request: proto.Request) !void { 830 @memcpy(&self.captured_fp, &sess.fingerprint); 831 switch (request) { 832 .exec => |cmd| { 833 // copy out of the protocol's stack-scoped buffer so the 834 // test can assert on it after the handler returns. 835 @memcpy(self.captured_exec_buf[0..cmd.len], cmd); 836 self.captured_exec_len = cmd.len; 837 }, 838 .shell => {}, 839 } 840 } 841 842 fn run(self: *ServerTask) void { 843 defer self.outgoing.close(self.io); 844 proto.handleConnection( 845 self.io, 846 self.allocator, 847 self.reader, 848 self.writer, 849 self.host_key, 850 null, 851 self, 852 ) catch |e| { 853 self.result = e; 854 }; 855 } 856 }; 857 858 // deterministic keys so failures don't depend on RNG state. 859 860 fn newHostKey() !proto.HostKey { 861 const seed = [_]u8{0xAA} ** Ed25519.KeyPair.seed_length; 862 return .{ .keypair = try Ed25519.KeyPair.generateDeterministic(seed) }; 863 } 864 865 fn newUserKeypair() !Ed25519.KeyPair { 866 const seed = [_]u8{0xBB} ** Ed25519.KeyPair.seed_length; 867 return try Ed25519.KeyPair.generateDeterministic(seed); 868 } 869 870 fn newClientEphemeral() !X25519.KeyPair { 871 const seed = [_]u8{0xCC} ** X25519.seed_length; 872 return try X25519.KeyPair.generateDeterministic(seed); 873 } 874