╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_ssh_protocol.zig ───────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1901 } 1902 1903 if (std.mem.eql(u8, req_type, "env")) { 1904 // ignore environment variables silently (we don't pass them anywhere) 1905 try replyChannelRequest(conn, ch, want_reply, true); 1906 return null; 1907 } 1908 1909 if (std.mem.eql(u8, req_type, "window-change")) { 1910 // uint32 width cells, uint32 height rows, uint32 width px, uint32 height px 1911 const sz = try takePtySize(&r); 1912 if (ch.pty != null) ch.pty = sz; 1913 // spec: window-change MUST NOT request a reply, but accept either 1914 try replyChannelRequest(conn, ch, want_reply, true); 1915 return null; 1916 } 1917 1918 if (std.mem.eql(u8, req_type, "shell")) { 1919 try replyChannelRequest(conn, ch, want_reply, true); 1920 return .{ .shell = ch.pty }; 1921 } 1922 1923 if (std.mem.eql(u8, req_type, "exec")) { 1924 // capture the command string for the handler to inspect 1925 const cmd = try takeStringField(allocator, &r, 4096); 1926 if (pending_exec.*) |old| allocator.free(old); 1927 pending_exec.* = cmd; 1928 try replyChannelRequest(conn, ch, want_reply, true); 1929 return .{ .exec = cmd }; 1930 } 1931 1932 // unknown request kind — fail it 1933 try replyChannelRequest(conn, ch, want_reply, false); 1934 return null; 1935 } 1936 1937 fn replyChannelRequest(conn: *Conn, ch: *Channel, want_reply: bool, success: bool) !void { 1938 if (!want_reply) return; 1939 var reply: std.ArrayList(u8) = .empty; 1940 defer reply.deinit(conn.allocator); 1941 try reply.append(conn.allocator, if (success) SSH_MSG_CHANNEL_SUCCESS else SSH_MSG_CHANNEL_FAILURE); 1942 try writeU32(&reply, conn.allocator, ch.remote_id); 1943 try conn.sc_cipher.writePacket(conn.io, conn.writer, reply.items); 1944 } 1945 1946 fn handleWindowAdjust(ch: *Channel, packet: []const u8) !void { 1947 // byte SSH_MSG_CHANNEL_WINDOW_ADJUST 1948 // uint32 recipient_channel 1949 // uint32 bytes_to_add 1950 var r = std.Io.Reader.fixed(packet[1..]); 1951 const recipient_channel = try r.takeInt(u32, .big); 1952 if (recipient_channel != ch.local_id) return error.UnknownChannel; 1953 const add = try r.takeInt(u32, .big); 1954 ch.remote_window +|= add; 1955 } 1956 1957 // charge a payload's bytes against the receive window without keeping them. 1958 // the caller credits the window back, choosing the budget that suits it. 1959 fn discardChannelData(ch: *Channel, packet: []const u8, extended: bool) !void { 1960 // byte SSH_MSG_CHANNEL_DATA/EXTENDED_DATA 1961 // uint32 recipient_channel 1962 // [if extended: uint32 type_code] 1963 // string data 1964 const payload = try parseChannelData(packet, extended, ch.local_id); 1965 const data_len: u32 = @intCast(payload.len); 1966 1967 if (data_len > ch.local_window) return error.WindowExceeded; 1968 ch.local_window -= data_len; 1969 } 1970 1971 pub fn parseChannelData(packet: []const u8, extended: bool, expected_channel: u32) ![]const u8 { 1972 if (packet.len < 1) return error.EmptyPacket; 1973 var r = std.Io.Reader.fixed(packet[1..]); 1974 const recipient_channel = try r.takeInt(u32, .big); 1975 if (recipient_channel != expected_channel) return error.UnknownChannel; 1976 if (extended) { 1977 const data_type = try r.takeInt(u32, .big); 1978 if (data_type != SSH_EXTENDED_DATA_STDERR) return error.UnsupportedExtendedData; 1979 } 1980 const data_len = try r.takeInt(u32, .big); 1981 // catches both data_len > remaining (would otherwise be a bounds-check 1982 // panic on the slice) and trailing junk after the declared payload. 1983 if (data_len != r.buffered().len) return error.InvalidChannelData; 1984 return r.buffered(); 1985 } 1986 1987 fn parseChannelId(packet: []const u8, expected_channel: u32) !void { 1988 if (packet.len < 1) return error.EmptyPacket; 1989 var r = std.Io.Reader.fixed(packet[1..]); 1990 const recipient_channel = try r.takeInt(u32, .big); 1991 if (recipient_channel != expected_channel) return error.UnknownChannel; 1992 if (r.buffered().len != 0) return error.InvalidChannelMessage; 1993 } 1994 1995 /// send a CHANNEL_WINDOW_ADJUST if our receive window has dipped below half 1996 /// of `budget`, the credit we're willing to have outstanding. used both for 1997 /// the pre-session discard path and by SessionCtx after it consumes a 1998 /// CHANNEL_DATA payload. 1999 fn maybeRefillRecvWindow(conn: *Conn, ch: *Channel, budget: u32) !void { 2000 if (ch.local_window >= budget / 2) return; 2001 const add = budget - ch.local_window; 2002 ch.local_window += add; 2003 var adj: std.ArrayList(u8) = .empty; 2004 defer adj.deinit(conn.allocator); 2005 try adj.append(conn.allocator, SSH_MSG_CHANNEL_WINDOW_ADJUST); 2006 try writeU32(&adj, conn.allocator, ch.remote_id); 2007 try writeU32(&adj, conn.allocator, add); 2008 try conn.sc_cipher.writePacket(conn.io, conn.writer, adj.items); 2009 } 2010 2011 pub fn writeU32(buf: *std.ArrayList(u8), allocator: std.mem.Allocator, value: u32) !void { 2012 var bytes: [4]u8 = undefined; 2013 std.mem.writeInt(u32, &bytes, value, .big); 2014 try buf.appendSlice(allocator, &bytes); 2015 } 2016