// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! The agent: the one process that ever holds the decrypted keys. //! //! It is started on demand by the first command that needs it and then //! lives on, detached, so that the master password is asked for once rather //! than at every commit. What it holds -- the user key, and the parsed //! OpenPGP keys -- lives in `secmem` memory and is wiped when the agent //! locks, which it does on request or after `lock_timeout_secs` without a //! request that used it. //! //! On Linux it can also be run by systemd, socket-activated: the //! `bwgpg-agent.socket` unit owns the socket and starts the agent on the //! first connection, handing it the listening descriptor. The agent then //! neither binds nor detaches, leaves the socket file to systemd when it //! stops, and exits once it locks after going unused, since systemd will //! start it again when it is next wanted. //! //! It also listens on the extra socket, `agent-extra.sock`, meant to be //! forwarded over SSH so that a remote machine can sign with the keys held //! here. A connection on it may only `describe` and `sign`; the environment //! it sends is ignored, since the remote machine's display and terminal mean //! nothing here; and each signature is confirmed on this machine unless //! `bwgpg remote allow` has been run for its SSH session. A session is the //! local `ssh` process carrying the forward, which `SO_PEERCRED` names. //! //! With `tailnet_port` set, or `bwgpg-agent-tailnet.socket` handing it a //! socket, it listens over the tailnet as well, for the same requests from //! a paired remote machine during an SSH session from here; `tailnet.zig` says //! what such a connection has to show before it is answered. The session is //! again the local `ssh`, found from the TCP connection the request names. //! //! The secret keys never leave it. A client asks which key a key selector means //! (`describe`), hashes its data itself, and sends only the digest to be //! signed (`sign`). const std = @import("std"); const Allocator = std.mem.Allocator; const Io = std.Io; const Environ = std.process.Environ; const log = std.log.scoped(.agent); const protocol = @import("protocol.zig"); const config_mod = @import("../config.zig"); const Config = config_mod.Config; const secmem = @import("../secmem.zig"); const platform = @import("../platform.zig"); const pinentry = @import("../pinentry.zig"); const api = @import("../bw/api.zig"); const bw_crypto = @import("../bw/crypto.zig"); const json = @import("../bw/json_util.zig"); const Keys = @import("../bw/vault.zig").Keys; const sign = @import("../openpgp/sign.zig"); const Sessions = @import("sessions.zig").Sessions; const tailnet = @import("tailnet.zig"); /// Which socket a connection came in on. pub const Kind = enum { /// `agent.sock`, for this machine. main, /// The extra socket, `agent-extra.sock`, for a remote machine over the /// forward. forward, /// A TCP socket on the tailnet, for a remote machine during an SSH /// session from this one. tailnet, }; /// What the agent knows of a connection's other end. const Conn = struct { kind: Kind, /// The process at the other end: the local `ssh` carrying the SSH /// session, for a remote machine's connection. pid: ?i32 = null, start_time: ?u64 = null, command: []const u8 = "?", /// The tailnet node, for a connection over the tailnet. node: ?[]const u8 = null, }; /// Reads a process's start time, for pruning sessions. const ProcProber = struct { io: Io, pub fn startTime(p: ProcProber, pid: i32) ?u64 { return platform.processStartTime(p.io, pid); } }; pub const Agent = struct { gpa: Allocator, io: Io, env: *const Environ.Map, config: *const Config, socket_path: []const u8, extra_socket_path: []const u8, /// Started by systemd on a socket it owns. socket_activated: bool = false, sessions: Sessions = .{}, mutex: Io.Mutex = .init, user_key: ?*bw_crypto.SymmetricKey = null, /// The keys found in the vault, while unlocked. keys: ?Keys = null, last_used: i64 = 0, fn now(agent: *Agent) i64 { return Io.Timestamp.now(agent.io, .real).toSeconds(); } /// Forgets everything, the allowed SSH sessions included. Called with /// the mutex held. fn lock(agent: *Agent) void { agent.sessions.clear(agent.gpa); if (agent.keys) |*k| k.deinit(secmem.allocator); agent.keys = null; if (agent.user_key) |k| { k.wipe(); secmem.allocator.destroy(k); } agent.user_key = null; } fn endpoints(agent: *Agent) !api.Endpoints { const c = agent.config; if (!c.allow_insecure_http and !std.mem.startsWith(u8, c.base_url, "https://")) { return error.InsecureServer; } var e = try api.Endpoints.fromBase(agent.gpa, c.base_url); if (c.identity_url) |u| { agent.gpa.free(e.identity); e.identity = try agent.gpa.dupe(u8, u); } if (c.api_url) |u| { agent.gpa.free(e.api); e.api = try agent.gpa.dupe(u8, u); } return e; } /// Fetches the vault and, if not already unlocked, asks for the master /// password to decrypt it. Called with the mutex held. fn syncVault(agent: *Agent, req_env: []const protocol.EnvVar, prompt: bool) !void { const gpa = agent.gpa; const e = try agent.endpoints(); defer e.deinit(gpa); var client = api.Client.init(gpa, agent.io, e); defer client.deinit(); const secret = try config_mod.clientSecret(gpa, agent.io, agent.config); defer { std.crypto.secureZero(u8, secret); gpa.free(secret); } const device_id = try config_mod.deviceId(gpa, agent.io, agent.env, agent.config); defer gpa.free(device_id); var token = try client.token(.{ .client_id = agent.config.client_id, .client_secret = secret, .device_id = device_id, }); defer token.deinit(gpa); const sync = try client.sync(token.access_token); defer sync.deinit(); if (agent.user_key == null) { if (!prompt) return error.Locked; const protected = token.protected_key orelse json.getString(json.get(sync.value, "profile") orelse .null, "key") orelse return error.UnexpectedResponse; const kdf = token.kdf orelse try client.prelogin(agent.config.email); agent.user_key = try agent.askPassword(req_env, protected, kdf); } const fresh = try Keys.load(secmem.allocator, sync.value, agent.user_key.?); if (agent.keys) |*old| old.deinit(secmem.allocator); agent.keys = fresh; for (fresh.unreadable.items) |u| { log.warn("Secure Note \"{s}\" looks like a key but cannot be read: {t}", .{ u.name, u.err }); } } /// The environment pinentry runs in: the agent's, with the requester's /// display and terminal laid over it. The caller deinits it. fn pinentryEnv(agent: *Agent, req_env: []const protocol.EnvVar) !Environ.Map { var env: Environ.Map = .init(agent.gpa); errdefer env.deinit(); for (agent.env.keys(), agent.env.values()) |k, v| try env.put(k, v); // Meant for this process alone; a pinentry that saw them might think // it had been socket-activated too. for ([_][]const u8{ "LISTEN_PID", "LISTEN_FDS", "LISTEN_FDNAMES" }) |name| { _ = env.swapRemove(name); } for (req_env) |v| try env.put(v.name, v.value); return env; } fn askPassword( agent: *Agent, req_env: []const protocol.EnvVar, protected: []const u8, kdf: bw_crypto.Kdf, ) !*bw_crypto.SymmetricKey { const gpa = agent.gpa; const io = agent.io; var env = try agent.pinentryEnv(req_env); defer env.deinit(); const program = try agent.pinentryProgram(); defer gpa.free(program); const description = try std.fmt.allocPrint( gpa, "Enter the master password for {s} on {s} to unlock your OpenPGP signing key.", .{ agent.config.email, agent.config.base_url }, ); defer gpa.free(description); const key = try secmem.allocator.create(bw_crypto.SymmetricKey); errdefer secmem.allocator.destroy(key); var error_text: ?[]const u8 = null; for (0..3) |_| { const password = try pinentry.getPin(secmem.allocator, io, program, &env, .{ .description = description, .error_text = error_text, .tty_name = env.get("GPG_TTY"), .tty_type = env.get("TERM"), }); defer secmem.allocator.free(password); var master = try bw_crypto.masterKey(secmem.allocator, io, password, agent.config.email, kdf); defer std.crypto.secureZero(u8, &master); var stretched = bw_crypto.stretch(master); defer stretched.wipe(); key.* = bw_crypto.decryptKey(secmem.allocator, &stretched, protected) catch |err| switch (err) { error.MacMismatch => { error_text = "Wrong master password. Try again."; continue; }, else => return err, }; return key; } return error.WrongPassword; } fn pinentryProgram(agent: *Agent) ![]u8 { return try platform.findPinentry(agent.gpa, agent.io, agent.env, agent.config.pinentry) orelse error.NoPinentry; } fn ensureUnlocked(agent: *Agent, req: protocol.Request) !*Keys { if (agent.keys == null) try agent.syncVault(req.env, true); agent.last_used = agent.now(); return &agent.keys.?; } fn keyList(agent: *Agent, arena: Allocator) ![]protocol.KeyInfo { const keys = agent.keys orelse return &.{}; var list: std.ArrayList(protocol.KeyInfo) = .empty; for (keys.notes.items) |note| { const fpr = note.key.primary().fingerprintHex(); try list.append(arena, .{ .fingerprint = try arena.dupe(u8, &fpr), .item = try arena.dupe(u8, note.name), .user_ids = try arena.dupe([]const u8, note.key.user_ids), }); } return list.toOwnedSlice(arena); } fn unreadableList(agent: *Agent, arena: Allocator) ![]const []const u8 { const keys = agent.keys orelse return &.{}; var list: std.ArrayList([]const u8) = .empty; for (keys.unreadable.items) |u| { try list.append(arena, try std.fmt.allocPrint(arena, "{s}: {t}", .{ u.name, u.err })); } return list.toOwnedSlice(arena); } fn sessionList(agent: *Agent, arena: Allocator) ![]protocol.SessionInfo { agent.sessions.prune(agent.gpa, ProcProber{ .io = agent.io }); const list = try arena.alloc(protocol.SessionInfo, agent.sessions.list.items.len); for (agent.sessions.list.items, list) |session, *info| { info.* = .{ .pid = session.pid, .command = try arena.dupe(u8, session.command), .allowed = session.allowed, }; } return list; } /// Asks on this machine whether a remote signature may be made, by either /// route. Called with the mutex held, so other requests wait for the /// answer, as they do for a password. fn confirmRemote(agent: *Agent, conn: Conn, found: Keys.Selected, summary: ?[]const u8) !bool { const gpa = agent.gpa; var env = try agent.pinentryEnv(&.{}); defer env.deinit(); const program = try agent.pinentryProgram(); defer gpa.free(program); const uid = if (found.note.key.user_ids.len > 0) found.note.key.user_ids[0] else found.note.name; const fpr = found.key.fingerprintHex(); var via_buf: [128]u8 = undefined; const via = if (conn.node) |node| std.fmt.bufPrint(&via_buf, ", from {s} over the tailnet", .{node}) catch "" else ""; const description = try std.fmt.allocPrint(gpa, \\A signature was requested over SSH ({s}){s}. \\ \\Key: {s} \\{s} \\ \\It says it is signing: {s} \\ \\To stop asking for this SSH session, run `bwgpg remote allow` on this machine. , .{ conn.command, via, uid, &fpr, summary orelse "(it did not say)" }); defer gpa.free(description); return pinentry.confirm(gpa, agent.io, program, &env, .{ .title = "bwgpg: sign for a remote machine?", .description = description, }); } /// Answers one request. Called with the mutex held. fn dispatch(agent: *Agent, arena: Allocator, conn: Conn, req_in: protocol.Request) !protocol.Response { const eql = std.mem.eql; var req = req_in; const op = req.op; if (conn.kind != .main) { // A remote machine may sign and ask which key a key selector means, // and nothing else; and its display and terminal mean nothing // here, so any prompt goes to this machine's own. if (!eql(u8, op, "ping") and !eql(u8, op, "describe") and !eql(u8, op, "sign")) { return error.NotPermittedRemotely; } req.env = &.{}; if (conn.pid) |pid| { agent.sessions.prune(agent.gpa, ProcProber{ .io = agent.io }); _ = try agent.sessions.touch(agent.gpa, pid, conn.start_time orelse 0, conn.command); } } if (eql(u8, op, "ping")) return .{ .ok = true }; if (eql(u8, op, "remote_list")) return .{ .ok = true, .sessions = try agent.sessionList(arena) }; if (eql(u8, op, "remote_allow") or eql(u8, op, "remote_revoke")) { agent.sessions.prune(agent.gpa, ProcProber{ .io = agent.io }); _ = agent.sessions.setAllowed(req.pid, eql(u8, op, "remote_allow")); return .{ .ok = true, .sessions = try agent.sessionList(arena) }; } if (eql(u8, op, "status")) return .{ .ok = true, .unlocked = agent.keys != null, .keys = try agent.keyList(arena), .problems = try agent.unreadableList(arena), }; if (eql(u8, op, "lock")) { agent.lock(); return .{ .ok = true }; } if (eql(u8, op, "unlock") or eql(u8, op, "sync")) { if (eql(u8, op, "unlock") and agent.keys != null) { // Already open; nothing to ask for. } else { try agent.syncVault(req.env, true); } agent.last_used = agent.now(); return .{ .ok = true, .unlocked = true, .keys = try agent.keyList(arena), .problems = try agent.unreadableList(arena), }; } if (eql(u8, op, "describe")) { const keys = try agent.ensureUnlocked(req); const selector = req.key orelse agent.config.default_key; const found = try keys.select(selector, config_mod.noteFor(agent.config, selector)); const fpr = found.key.fingerprintHex(); return .{ .ok = true, .fingerprint = try arena.dupe(u8, &fpr), .algorithm = @backingInt(found.key.algorithm), }; } if (eql(u8, op, "sign")) { const keys = try agent.ensureUnlocked(req); var fpr: [20]u8 = undefined; const fpr_hex = req.fingerprint orelse return error.BadRequest; if (fpr_hex.len != 40) return error.BadRequest; _ = std.fmt.hexToBytes(&fpr, fpr_hex) catch return error.BadRequest; const found = keys.find(fpr) orelse return error.NoSigningKey; const key = found.key; var digest: [sign.digest_length]u8 = undefined; const digest_b64 = req.digest orelse return error.BadRequest; const decoder = std.base64.standard.Decoder; if ((decoder.calcSizeForSlice(digest_b64) catch return error.BadRequest) != digest.len) { return error.BadRequest; } decoder.decode(&digest, digest_b64) catch return error.BadRequest; if (conn.kind != .main and agent.config.remote_confirm) { const allowed = if (conn.pid) |pid| for (agent.sessions.list.items) |session| { if (session.pid == pid and session.start_time == conn.start_time) break session.allowed; } else false else false; if (!allowed and !try agent.confirmRemote(conn, found, req.summary)) { log.info("denied a signature for {s}", .{conn.command}); return error.Denied; } } var material_buf: [sign.max_material]u8 = undefined; const material = try sign.signDigest(key, digest, &material_buf); const encoded = try arena.alloc(u8, std.base64.standard.Encoder.calcSize(material.len)); return .{ .ok = true, .material = std.base64.standard.Encoder.encode(encoded, material) }; } if (eql(u8, op, "stop")) return .{ .ok = true }; return error.BadRequest; } fn handle(agent: *Agent, stream: Io.net.Stream, kind: Kind) void { const io = agent.io; defer stream.close(io); switch (kind) { .main, .forward => if (!platform.peerIsSelf(stream)) { log.warn("refused a connection from another user", .{}); return; }, // Before reading a word of it: systemd's socket listens on every // interface, and only the firewall keeps the rest of the world // off it. .tailnet => if (!tailnet.isTailnet(stream.socket.address)) { log.warn("refused a connection from {f}, which is not on the tailnet", .{stream.socket.address}); return; }, } var arena_state: std.heap.ArenaAllocator = .init(agent.gpa); defer arena_state.deinit(); const arena = arena_state.allocator(); var read_buf: [4096]u8 = undefined; var write_buf: [4096]u8 = undefined; var reader = stream.reader(io, &read_buf); var writer = stream.writer(io, &write_buf); const parsed = protocol.receive(protocol.Request, arena, &reader.interface) catch |err| { log.warn("unreadable request: {t}", .{err}); return; }; const req = parsed.value; var conn: Conn = .{ .kind = kind }; if (kind == .forward) { if (platform.peerOf(stream)) |peer| { conn.pid = peer.pid; conn.start_time = platform.processStartTime(io, peer.pid); conn.command = platform.processCommand(arena, io, peer.pid) catch "?"; } } const response: ?protocol.Response = if (kind == .tailnet) admitted: { conn = agent.admitTailnet(arena, stream.socket.address, req) catch |err| switch (err) { // Not a node of the tailnet: nothing to say to it. error.NotOnTailnet => return, else => { log.warn("refused a request over the tailnet from {f}: {t}", .{ stream.socket.address, err }); break :admitted errorResponse(err); }, }; break :admitted null; } else null; const answer = response orelse blk: { agent.mutex.lockUncancelable(io); defer agent.mutex.unlock(io); break :blk agent.dispatch(arena, conn, req) catch |err| errorResponse(err); }; protocol.send(arena, &writer.interface, answer) catch |err| { log.warn("could not answer: {t}", .{err}); }; if (kind == .main and std.mem.eql(u8, req.op, "stop")) agent.shutdown(); } /// Decides whether a request over the tailnet, from the address `from`, /// is to be answered, and for which SSH session; see `tailnet.zig`. Called /// without the mutex: it asks tailscaled and reads `/proc`, and holds up /// no one else meanwhile. fn admitTailnet(agent: *Agent, arena: Allocator, from: Io.net.IpAddress, req: protocol.Request) !Conn { const io = agent.io; const node = tailnet.whois(arena, io, tailnet.tailscaledSocket(agent.env), from) catch |err| switch (err) { error.TailscaledRefused => return error.NotOnTailnet, else => return err, }; const pairings_path = try tailnet.remotePairingsPath(arena, io, agent.env); const pairings = try tailnet.load(tailnet.RemotePairings, arena, io, pairings_path); if (!tailnet.tokenMatches(pairings, node.stable_id, req.token orelse "")) return error.NotPaired; // The session the request names has to be one from here to the // remote machine asking: from an address of this machine's, which the // connection existing here shows, to one of that node's. const sc = tailnet.parseSshConnection(req.ssh orelse "") orelse return error.NoSshSession; if (!node.has(sc.server)) return error.NoSshSession; const pid = try tailnet.sessionProcess(arena, io, sc.client, sc.server) orelse return error.NoSshSession; return .{ .kind = .tailnet, .pid = pid, .start_time = platform.processStartTime(io, pid), .command = platform.processCommand(arena, io, pid) catch "?", .node = node.dns_name, }; } fn shutdown(agent: *Agent) noreturn { agent.mutex.lockUncancelable(agent.io); agent.lock(); // systemd's socket stays, so that the next connection starts the // agent again. if (!agent.socket_activated) { Io.Dir.cwd().deleteFile(agent.io, agent.socket_path) catch {}; Io.Dir.cwd().deleteFile(agent.io, agent.extra_socket_path) catch {}; } log.info("stopped", .{}); std.process.exit(0); } fn watchdog(agent: *Agent) void { const io = agent.io; const timeout: i64 = agent.config.lock_timeout_secs; while (true) { io.sleep(.fromSeconds(10), .awake) catch return; agent.mutex.lockUncancelable(io); defer agent.mutex.unlock(io); if (agent.keys != null and agent.now() - agent.last_used >= timeout) { agent.lock(); log.info("locked after {d} seconds unused", .{timeout}); // Locked, it holds nothing worth keeping a process for, and // systemd starts another on the next connection. if (agent.socket_activated) { log.info("exiting until the socket is next used", .{}); std.process.exit(0); } } } } }; fn errorResponse(err: anyerror) protocol.Response { const code: []const u8, const message: []const u8 = switch (err) { error.Cancelled => .{ "cancelled", "The password prompt was cancelled." }, error.WrongPassword => .{ "wrong_password", "The master password was wrong three times." }, error.NoPinentry => .{ "no_pinentry", "The agent found no pinentry on its PATH; set \"pinentry\" in the config, or BWGPG_PINENTRY in the agent's environment." }, error.BadCredentials => .{ "bad_credentials", "The server refused the API key; run `bwgpg login` again." }, error.InsecureServer => .{ "insecure_server", "The server URL is not https." }, error.NoClientSecret => .{ "no_client_secret", "The config has no client_secret, or client_secret_file cannot be read." }, error.InsecurePermissions => .{ "insecure_permissions", "A file of the agent's holding a secret -- the client_secret_file, or tailnet-peers.json -- is readable by others; chmod 600 it." }, error.NoSigningKey => .{ "no_key", "No key in the vault matches, or none that matches can sign." }, error.AmbiguousKey => .{ "ambiguous", "The vault holds more than one key; say which with -u or default_key." }, error.Locked => .{ "locked", "The agent is locked." }, error.BadRequest => .{ "bad_request", "The request was not understood." }, error.NotPermittedRemotely => .{ "not_permitted_remotely", "The agent does not do that for a remote machine." }, error.NotPaired => .{ "not_paired", "This machine is not paired with the agent over the tailnet, or its token is wrong; run `bwgpg remote pair` for it on the key machine." }, error.NoSshSession => .{ "no_ssh_session", "The agent found no SSH session of its user's from the key machine to this one to sign for." }, error.TailscaledUnreachable, error.TailscaledUnreadable => .{ "tailscaled", "The agent could not ask tailscaled who this machine is." }, error.InvalidPairingFile => .{ "invalid_pairing_file", "The agent's tailnet-peers.json cannot be read." }, error.Denied => .{ "denied", "The signature was denied on the key machine." }, error.UnsupportedEncStringType => .{ "unsupported_encryption", "The account uses an encryption type bwgpg does not support." }, error.ConnectionRefused, error.NetworkUnreachable, error.HostUnreachable, error.UnknownHostName, error.NameServerFailure, error.TemporaryNameServerFailure, error.ConnectionTimedOut => .{ "unreachable", "The Bitwarden server could not be reached." }, else => .{ "failed", @errorName(err) }, }; return .{ .ok = false, .@"error" = code, .message = message }; } /// Runs the agent until it is told to stop. Returns only on failure to /// start. /// Accepts connections on one socket, forever, each on a thread of its own: /// a client which connects and says nothing holds up only itself. Requests /// still take turns, and one waiting on a pinentry holds the others until it /// is answered. fn acceptLoop(agent: *Agent, server: *Io.net.Server, kind: Kind) void { const io = agent.io; while (true) { const stream = server.accept(io) catch |err| switch (err) { error.ConnectionAborted, error.WouldBlock, error.ProtocolFailure => continue, else => { log.err("cannot accept on the {t} socket: {t}", .{ kind, err }); std.process.exit(1); }, }; const thread = std.Thread.spawn(.{}, Agent.handle, .{ agent, stream, kind }) catch { agent.handle(stream, kind); continue; }; thread.detach(); } } /// What `UnixAddress.listen` would have returned, for a socket that systemd /// bound instead. fn serverFromFd(fd: Io.net.Socket.Handle) Io.net.Server { return .{ .socket = .{ .handle = fd, .address = .{ .ip4 = .loopback(0) } }, .options = if (Io.net.Server.AcceptOptions != void) .{ .mode = .stream, .protocol = null } else {}, }; } fn bindFresh(io: Io, path: []const u8) !Io.net.Server { const address = try Io.net.UnixAddress.init(path); Io.Dir.cwd().deleteFile(io, path) catch {}; return address.listen(io, .{}); } /// Runs the agent until it is told to stop. Returns only on failure to /// start. pub fn run(gpa: Allocator, io: Io, env: *const Environ.Map, config: *const Config, foreground: bool) !void { secmem.hardenProcess(); const dir = try config_mod.runtimeDir(gpa, io, env); defer gpa.free(dir); const socket_path = try std.fs.path.join(gpa, &.{ dir, "agent.sock" }); defer gpa.free(socket_path); const extra_socket_path = try std.fs.path.join(gpa, &.{ dir, "agent-extra.sock" }); defer gpa.free(extra_socket_path); var main_server: Io.net.Server = undefined; var extra_server: ?Io.net.Server = null; const fds = try platform.systemdListenFds(env); // systemd hands sockets only on Linux; elsewhere this is never taken, // and is not compiled. if (platform.is_linux and fds.any()) { main_server = serverFromFd(fds.main orelse return error.NoMainSocket); if (fds.extra) |fd| extra_server = serverFromFd(fd); log.info("listening on socket from systemd ({s}{s})", .{ socket_path, if (fds.extra != null) ", and the extra socket for SSH" else "", }); } else { if (!foreground) platform.detach(); try config_mod.makePrivateDir(io, dir); // Another agent already answering is not an error: there should be // one, and there is. if ((try Io.net.UnixAddress.init(socket_path)).connect(io)) |stream| { stream.close(io); log.info("an agent is already running at {s}", .{socket_path}); return; } else |_| {} main_server = try bindFresh(io, socket_path); // The extra socket is for forwarding over SSH, which bwgpg supports // from Linux. if (platform.is_windows) { log.info("listening at {s}", .{socket_path}); } else if (bindFresh(io, extra_socket_path)) |extra| { extra_server = extra; log.info("listening at {s} and {s}", .{ socket_path, extra_socket_path }); } else |err| { // Signing over SSH is an extra; the agent works without it. log.warn("cannot listen at {s} ({t}); signing over SSH is not available", .{ extra_socket_path, err }); log.info("listening at {s}", .{socket_path}); } } defer main_server.deinit(io); defer if (extra_server) |*e| e.deinit(io); // Over the tailnet: systemd's socket, or one on each of this machine's // tailnet addresses when `tailnet_port` is set and the agent binds its // own. Like the extra socket, an extra: the agent works without it. var tailnet_servers: [4]Io.net.Server = undefined; var tailnet_count: usize = 0; defer for (tailnet_servers[0..tailnet_count]) |*t| t.deinit(io); if (platform.is_linux and fds.tailnet != null) { tailnet_servers[0] = serverFromFd(fds.tailnet.?); tailnet_count = 1; log.info("listening on the tailnet, on a socket from systemd", .{}); } else if (config.tailnet_port) |port| if (!fds.any()) { tailnet_count = listenTailnet(gpa, io, env, port, &tailnet_servers); }; var agent: Agent = .{ .gpa = gpa, .io = io, .env = env, .config = config, .socket_path = socket_path, .extra_socket_path = extra_socket_path, .socket_activated = fds.any(), }; const dog = try std.Thread.spawn(.{}, Agent.watchdog, .{&agent}); dog.detach(); if (extra_server) |*e| { const t = try std.Thread.spawn(.{}, acceptLoop, .{ &agent, e, Kind.forward }); t.detach(); } for (tailnet_servers[0..tailnet_count]) |*server| { const t = try std.Thread.spawn(.{}, acceptLoop, .{ &agent, server, Kind.tailnet }); t.detach(); } acceptLoop(&agent, &main_server, .main); } /// Listens on `port` at each of this machine's tailnet addresses, as /// tailscaled reports them, into `servers`. Returns how many it managed, /// having said why when that is none. fn listenTailnet(gpa: Allocator, io: Io, env: *const Environ.Map, port: u16, servers: []Io.net.Server) usize { var arena_state: std.heap.ArenaAllocator = .init(gpa); defer arena_state.deinit(); const status = tailnet.status(arena_state.allocator(), io, tailnet.tailscaledSocket(env)) catch |err| { log.warn("cannot ask tailscaled for this machine's addresses ({t}); signing over the tailnet is not available", .{err}); return 0; }; var count: usize = 0; for (status.self.addresses) |address| { if (count == servers.len) break; var a = address; a.setPort(port); servers[count] = a.listen(io, .{ .reuse_address = true }) catch |err| { log.warn("cannot listen at {f} ({t})", .{ a, err }); continue; }; log.info("listening on the tailnet at {f}", .{a}); count += 1; } if (count == 0) log.warn("signing over the tailnet is not available", .{}); return count; } /// tailscaled, as far as `whois` goes: answers every request with `body`, /// or with a 404 when it is null. const FakeTailscaled = struct { server: Io.net.Server, body: ?[]const u8, fn run(f: *FakeTailscaled, io: Io) void { while (true) { const stream = f.server.accept(io) catch return; defer stream.close(io); var read_buf: [1024]u8 = undefined; var reader = stream.reader(io, &read_buf); // The head ends with an empty line. while (reader.interface.takeDelimiterInclusive('\n')) |line| { if (std.mem.eql(u8, line, "\r\n")) break; } else |_| return; var write_buf: [1024]u8 = undefined; var writer = stream.writer(io, &write_buf); if (f.body) |b| { writer.interface.print("HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n{s}", .{b}) catch return; } else { writer.interface.writeAll("HTTP/1.0 404 Not Found\r\n\r\nno match for IP:port\n") catch return; } writer.interface.flush() catch return; } } }; test "who is answered over the tailnet" { if (!platform.is_linux and !platform.is_windows) return error.SkipZigTest; const gpa = std.testing.allocator; var threaded: Io.Threaded = .init(gpa, .{}); defer threaded.deinit(); const io = threaded.io(); var arena_state: std.heap.ArenaAllocator = .init(gpa); defer arena_state.deinit(); const arena = arena_state.allocator(); var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); const dir = try tmp.dir.realPathFileAlloc(io, ".", arena); const socket = try std.fs.path.join(arena, &.{ dir, "tailscaled.sock" }); // A node whose address is the loopback, so that a TCP connection made // here can stand for the SSH session to it. var fake: FakeTailscaled = .{ // Wine refuses the request Zig's Windows `listen` names a Unix // socket's path with; skipped there, as in client.zig. .server = (try Io.net.UnixAddress.init(socket)).listen(io, .{}) catch |err| return switch (err) { error.Unexpected => if (platform.is_windows) error.SkipZigTest else err, else => err, }, .body = \\{"Node":{"StableID":"12","Name":"b.tailnet.example.","Addresses":["127.0.0.1/32"]}} , }; defer fake.server.deinit(io); var group: Io.Group = .init; defer group.cancel(io); try group.concurrent(io, FakeTailscaled.run, .{ &fake, io }); var env: Environ.Map = .init(gpa); defer env.deinit(); try env.put("BWGPG_TAILSCALED_SOCKET", socket); try env.put("BWGPG_STATE_DIR", dir); const config: Config = .{ .base_url = "https://vault.example", .email = "a@example", .client_id = "user.x" }; var agent: Agent = .{ .gpa = gpa, .io = io, .env = &env, .config = &config, .socket_path = "", .extra_socket_path = "", }; // The SSH session: a connection from this process to itself. var listener = try (try Io.net.IpAddress.parse("127.0.0.1", 0)).listen(io, .{}); defer listener.deinit(io); const ssh_stream = try listener.socket.address.connect(io, .{ .mode = .stream }); defer ssh_stream.close(io); const accepted = try listener.accept(io); defer accepted.close(io); const ssh = try std.fmt.allocPrint(arena, "127.0.0.1 {d} 127.0.0.1 {d}", .{ accepted.socket.address.getPort(), listener.socket.address.getPort(), }); const from = try Io.net.IpAddress.parse("100.64.0.7", 41000); const token = tailnet.newToken(io); var req: protocol.Request = .{ .op = "sign", .token = &token, .ssh = ssh }; // Not paired yet. try std.testing.expectError(error.NotPaired, agent.admitTailnet(arena, from, req)); const peers_path = try tailnet.remotePairingsPath(arena, io, &env); const hash = tailnet.tokenHash(&token).?; try config_mod.savePrivate(gpa, io, peers_path, tailnet.RemotePairings{ .peers = &.{ .{ .node = "b", .stable_id = "12", .token_sha256 = &hash }, } }); const conn = try agent.admitTailnet(arena, from, req); try std.testing.expectEqual(Kind.tailnet, conn.kind); try std.testing.expectEqual(@as(?i32, platform.currentPid()), conn.pid); try std.testing.expectEqualStrings("b.tailnet.example", conn.node.?); // The wrong token, no session, a session to somewhere else, and a // session that is not there. const wrong = tailnet.newToken(io); req.token = &wrong; try std.testing.expectError(error.NotPaired, agent.admitTailnet(arena, from, req)); req.token = &token; req.ssh = null; try std.testing.expectError(error.NoSshSession, agent.admitTailnet(arena, from, req)); req.ssh = try std.fmt.allocPrint(arena, "127.0.0.1 {d} 127.0.0.2 {d}", .{ accepted.socket.address.getPort(), listener.socket.address.getPort(), }); try std.testing.expectError(error.NoSshSession, agent.admitTailnet(arena, from, req)); req.ssh = "127.0.0.1 1 127.0.0.1 2"; try std.testing.expectError(error.NoSshSession, agent.admitTailnet(arena, from, req)); // An address tailscaled does not know. fake.body = null; req.ssh = ssh; try std.testing.expectError(error.NotOnTailnet, agent.admitTailnet(arena, from, req)); }