@@ -207,29 +207,46 @@ int ssh_start_isolated_agent(ssh_config_t *ssh_config, const account_t *account) |
| 207 | 207 | char command[512]; |
| 208 | 208 | char output[1024]; |
| 209 | 209 | char socket_dir[MAX_PATH_LEN]; |
| 210 | | - |
| 210 | + char socket_path[MAX_PATH_LEN]; |
| 211 | + |
| 211 | 212 | if (!ssh_config || !account) { |
| 212 | 213 | set_error(ERR_INVALID_ARGS, "Invalid arguments to ssh_start_isolated_agent"); |
| 213 | 214 | return -1; |
| 214 | 215 | } |
| 215 | | - |
| 216 | + |
| 216 | 217 | log_info("Starting isolated SSH agent for account: %s", account->name); |
| 217 | | - |
| 218 | + |
| 218 | 219 | /* Stop any existing agent we own */ |
| 219 | 220 | if (ssh_config->agent_owned && ssh_config->agent_pid > 0) { |
| 220 | 221 | log_debug("Stopping existing SSH agent"); |
| 221 | 222 | ssh_stop_agent(ssh_config); |
| 222 | 223 | } |
| 223 | | - |
| 224 | + |
| 224 | 225 | /* Create secure socket directory */ |
| 225 | 226 | if (create_isolated_agent_socket_dir(socket_dir, sizeof(socket_dir)) != 0) { |
| 226 | 227 | return -1; |
| 227 | 228 | } |
| 228 | | - |
| 229 | + |
| 230 | + /* Build socket path and check for stale sockets */ |
| 231 | + if ((size_t)snprintf(socket_path, sizeof(socket_path), |
| 232 | + "%s/ssh-agent.%s.sock", |
| 233 | + socket_dir, account->name) >= sizeof(socket_path)) { |
| 234 | + set_error(ERR_INVALID_ARGS, "SSH socket path too long"); |
| 235 | + return -1; |
| 236 | + } |
| 237 | + |
| 238 | + /* Remove stale socket if it exists */ |
| 239 | + if (path_exists(socket_path)) { |
| 240 | + log_debug("Removing stale SSH agent socket: %s", socket_path); |
| 241 | + if (unlink(socket_path) != 0) { |
| 242 | + set_system_error(ERR_FILE_IO, "Failed to remove stale SSH socket"); |
| 243 | + return -1; |
| 244 | + } |
| 245 | + } |
| 246 | + |
| 229 | 247 | /* Build ssh-agent command with socket path */ |
| 230 | | - if ((size_t)snprintf(command, sizeof(command), |
| 231 | | - "ssh-agent -a '%s/ssh-agent.%s.sock'", |
| 232 | | - socket_dir, account->name) >= sizeof(command)) { |
| 248 | + if ((size_t)snprintf(command, sizeof(command), |
| 249 | + "ssh-agent -a '%s'", socket_path) >= sizeof(command)) { |
| 233 | 250 | set_error(ERR_INVALID_ARGS, "SSH agent command too long"); |
| 234 | 251 | return -1; |
| 235 | 252 | } |