|
| 1 | +// |
| 2 | +// AgentAuthenticator.swift |
| 3 | +// TablePro |
| 4 | +// |
| 5 | + |
| 6 | +import Foundation |
| 7 | +import os |
| 8 | + |
| 9 | +import CLibSSH2 |
| 10 | + |
| 11 | +internal struct AgentAuthenticator: SSHAuthenticator { |
| 12 | + private static let logger = Logger(subsystem: "com.TablePro", category: "AgentAuthenticator") |
| 13 | + |
| 14 | + /// Protects setenv/unsetenv of SSH_AUTH_SOCK across concurrent tunnel setups |
| 15 | + private static let agentSocketLock = NSLock() |
| 16 | + |
| 17 | + let socketPath: String? |
| 18 | + |
| 19 | + func authenticate(session: OpaquePointer, username: String) throws { |
| 20 | + // Save original SSH_AUTH_SOCK so we can restore it |
| 21 | + let originalSocketPath = ProcessInfo.processInfo.environment["SSH_AUTH_SOCK"] |
| 22 | + let needsSocketOverride = socketPath != nil |
| 23 | + |
| 24 | + if let overridePath = socketPath, needsSocketOverride { |
| 25 | + Self.agentSocketLock.lock() |
| 26 | + Self.logger.debug("Using custom SSH agent socket: \(overridePath, privacy: .private)") |
| 27 | + setenv("SSH_AUTH_SOCK", overridePath, 1) |
| 28 | + } |
| 29 | + |
| 30 | + defer { |
| 31 | + if needsSocketOverride { |
| 32 | + // Restore original SSH_AUTH_SOCK |
| 33 | + if let originalSocketPath { |
| 34 | + setenv("SSH_AUTH_SOCK", originalSocketPath, 1) |
| 35 | + } else { |
| 36 | + unsetenv("SSH_AUTH_SOCK") |
| 37 | + } |
| 38 | + Self.agentSocketLock.unlock() |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + guard let agent = libssh2_agent_init(session) else { |
| 43 | + throw SSHTunnelError.tunnelCreationFailed("Failed to initialize SSH agent") |
| 44 | + } |
| 45 | + |
| 46 | + defer { |
| 47 | + libssh2_agent_disconnect(agent) |
| 48 | + libssh2_agent_free(agent) |
| 49 | + } |
| 50 | + |
| 51 | + var rc = libssh2_agent_connect(agent) |
| 52 | + guard rc == 0 else { |
| 53 | + Self.logger.error("Failed to connect to SSH agent (rc=\(rc))") |
| 54 | + throw SSHTunnelError.tunnelCreationFailed("Failed to connect to SSH agent") |
| 55 | + } |
| 56 | + |
| 57 | + rc = libssh2_agent_list_identities(agent) |
| 58 | + guard rc == 0 else { |
| 59 | + Self.logger.error("Failed to list SSH agent identities (rc=\(rc))") |
| 60 | + throw SSHTunnelError.tunnelCreationFailed("Failed to list SSH agent identities") |
| 61 | + } |
| 62 | + |
| 63 | + // Iterate through available identities and try each |
| 64 | + var previousIdentity: UnsafeMutablePointer<libssh2_agent_publickey>? |
| 65 | + var currentIdentity: UnsafeMutablePointer<libssh2_agent_publickey>? |
| 66 | + |
| 67 | + while true { |
| 68 | + rc = libssh2_agent_get_identity(agent, ¤tIdentity, previousIdentity) |
| 69 | + |
| 70 | + if rc == 1 { |
| 71 | + // End of identity list, none worked |
| 72 | + break |
| 73 | + } |
| 74 | + if rc < 0 { |
| 75 | + Self.logger.error("Failed to get SSH agent identity (rc=\(rc))") |
| 76 | + throw SSHTunnelError.tunnelCreationFailed("Failed to get SSH agent identity") |
| 77 | + } |
| 78 | + |
| 79 | + guard let identity = currentIdentity else { |
| 80 | + break |
| 81 | + } |
| 82 | + |
| 83 | + let authRc = libssh2_agent_userauth(agent, username, identity) |
| 84 | + if authRc == 0 { |
| 85 | + Self.logger.info("SSH agent authentication succeeded") |
| 86 | + return |
| 87 | + } |
| 88 | + |
| 89 | + previousIdentity = identity |
| 90 | + } |
| 91 | + |
| 92 | + Self.logger.error("SSH agent authentication failed: no identity accepted") |
| 93 | + throw SSHTunnelError.authenticationFailed |
| 94 | + } |
| 95 | +} |
0 commit comments