Something went wrong. Try again.
Sign git commits and tags with an OpenPGP key kept in Bitwarden.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! The SSH sessions remote signing goes through, by either route.//!//! A remote machine's request on the forward arrives on the extra socket//! through the local `ssh` process carrying the forward, and `SO_PEERCRED`//! names that process; on the tailnet, the agent finds the same process from//! the TCP connection the request names. So one `ssh` process is one session://! every request from it, however many connections, shares its entry here. An//! entry is keyed by the pid and the process's start time together, because a//! pid alone can be reused by an unrelated process once the first has exited.//!//! A session starts out asking for confirmation of every signature, and `bwgpg//! remote allow` makes it allowed for as long as it lasts. It ends when its//! process does, and the table forgets it the next time it is looked at.
const std = @import("std");const Allocator = std.mem.Allocator;const testing = std.testing;
pub const Session = struct { pid: i32, start_time: u64, /// The `ssh` command line, for showing to the user. Owned. command: []u8, allowed: bool = false,};
pub const Sessions = struct { list: std.ArrayList(Session) = .empty,
pub fn deinit(s: *Sessions, gpa: Allocator) void { s.clear(gpa); s.list.deinit(gpa); }
pub fn clear(s: *Sessions, gpa: Allocator) void { for (s.list.items) |session| gpa.free(session.command); s.list.clearRetainingCapacity(); }
/// The session for this process, made if there is none. `command` is /// copied only when a new session is made. pub fn touch(s: *Sessions, gpa: Allocator, pid: i32, start_time: u64, command: []const u8) Allocator.Error!*Session { for (s.list.items, 0..) |*session, i| { if (session.pid != pid) continue; if (session.start_time == start_time) return session; // The same pid, a different process: the old one is gone. gpa.free(session.command); _ = s.list.swapRemove(i); break; } const owned = try gpa.dupe(u8, command); errdefer gpa.free(owned); try s.list.append(gpa, .{ .pid = pid, .start_time = start_time, .command = owned }); return &s.list.items[s.list.items.len - 1]; }
/// Forgets the sessions whose process has ended. `prober` has a /// `startTime(pid) ?u64` method, which is how a process is found alive: /// its pid still answers, with the start time the session was made with. pub fn prune(s: *Sessions, gpa: Allocator, prober: anytype) void { var i: usize = 0; while (i < s.list.items.len) { const session = s.list.items[i]; if (prober.startTime(session.pid) == session.start_time) { i += 1; continue; } gpa.free(session.command); _ = s.list.swapRemove(i); } }
/// Sets `allowed` on the session with this pid, or on all of them when /// `pid` is null. Returns how many were changed. pub fn setAllowed(s: *Sessions, pid: ?i32, allowed: bool) usize { var n: usize = 0; for (s.list.items) |*session| { if (pid) |p| { if (session.pid != p) continue; } if (session.allowed != allowed) n += 1; session.allowed = allowed; } return n; }};
const FakeProber = struct { alive: []const [2]u64,
fn startTime(p: FakeProber, pid: i32) ?u64 { for (p.alive) |entry| { if (entry[0] == @as(u64, @intCast(pid))) return entry[1]; } return null; }};
test "a session is one process, for as long as it lives" { const gpa = testing.allocator; var s: Sessions = .{}; defer s.deinit(gpa);
const a = try s.touch(gpa, 100, 5000, "ssh hostb"); try testing.expect(!a.allowed); // The same process again is the same session. try testing.expectEqual(a, try s.touch(gpa, 100, 5000, "ignored")); _ = try s.touch(gpa, 200, 6000, "ssh hostc"); try testing.expectEqual(@as(usize, 2), s.list.items.len);
// Allowing one, then all. try testing.expectEqual(@as(usize, 1), s.setAllowed(100, true)); try testing.expectEqual(@as(usize, 1), s.setAllowed(null, true)); try testing.expectEqual(@as(usize, 2), s.setAllowed(null, false));
// pid 100 reused by a new process: a new session, not an allowed one. _ = s.setAllowed(100, true); const reused = try s.touch(gpa, 100, 7000, "ssh hostd"); try testing.expect(!reused.allowed); try testing.expectEqualStrings("ssh hostd", reused.command); try testing.expectEqual(@as(usize, 2), s.list.items.len);
// Pruning keeps only what is still alive with the same start time. s.prune(gpa, FakeProber{ .alive = &.{ .{ 100, 7000 }, .{ 200, 1 } } }); try testing.expectEqual(@as(usize, 1), s.list.items.len); try testing.expectEqual(@as(i32, 100), s.list.items[0].pid);}