@@ -2,6 +2,9 @@ |
| 2 | 2 | * Implements secure account switching and management for gitswitch-c |
| 3 | 3 | */ |
| 4 | 4 | |
| 5 | +/* Enable POSIX extensions for setenv/unsetenv */ |
| 6 | +#define _POSIX_C_SOURCE 200809L |
| 7 | + |
| 5 | 8 | #include <stdio.h> |
| 6 | 9 | #include <stdlib.h> |
| 7 | 10 | #include <string.h> |
@@ -18,6 +21,20 @@ |
| 18 | 21 | #include "ssh_manager.h" |
| 19 | 22 | #include "gpg_manager.h" |
| 20 | 23 | |
| 24 | +/* Active session state - tracks SSH/GPG resources for proper cleanup */ |
| 25 | +typedef struct { |
| 26 | + ssh_config_t ssh_config; |
| 27 | + gpg_config_t gpg_config; |
| 28 | + bool ssh_active; |
| 29 | + bool gpg_active; |
| 30 | + char original_gnupghome[MAX_PATH_LEN]; |
| 31 | + bool had_original_gnupghome; |
| 32 | + bool gnupghome_saved; |
| 33 | +} active_session_t; |
| 34 | + |
| 35 | +/* Static session state - only one active session at a time */ |
| 36 | +static active_session_t g_session = {0}; |
| 37 | + |
| 21 | 38 | /* Internal helper functions */ |
| 22 | 39 | static uint32_t get_next_available_id(const gitswitch_ctx_t *ctx); |
| 23 | 40 | static int validate_ssh_key_security(const char *ssh_key_path); |
@@ -31,16 +48,54 @@ int accounts_init(gitswitch_ctx_t *ctx) { |
| 31 | 48 | set_error(ERR_INVALID_ARGS, "NULL context to accounts_init"); |
| 32 | 49 | return -1; |
| 33 | 50 | } |
| 34 | | - |
| 51 | + |
| 35 | 52 | /* Initialize account array */ |
| 36 | 53 | memset(ctx->accounts, 0, sizeof(ctx->accounts)); |
| 37 | 54 | ctx->account_count = 0; |
| 38 | 55 | ctx->current_account = NULL; |
| 39 | | - |
| 56 | + |
| 57 | + /* Initialize session state */ |
| 58 | + memset(&g_session, 0, sizeof(g_session)); |
| 59 | + |
| 40 | 60 | log_debug("Accounts system initialized"); |
| 41 | 61 | return 0; |
| 42 | 62 | } |
| 43 | 63 | |
| 64 | +/* Clean up active session resources */ |
| 65 | +void accounts_session_cleanup(void) { |
| 66 | + log_debug("Cleaning up active session resources"); |
| 67 | + |
| 68 | + /* Clean up SSH agent if we started one */ |
| 69 | + if (g_session.ssh_active) { |
| 70 | + log_info("Stopping SSH agent (pid=%d)", g_session.ssh_config.agent_pid); |
| 71 | + ssh_manager_cleanup(&g_session.ssh_config); |
| 72 | + g_session.ssh_active = false; |
| 73 | + } |
| 74 | + |
| 75 | + /* Clean up GPG environment if we modified it */ |
| 76 | + if (g_session.gpg_active) { |
| 77 | + log_info("Cleaning up GPG environment"); |
| 78 | + gpg_manager_cleanup(&g_session.gpg_config); |
| 79 | + g_session.gpg_active = false; |
| 80 | + } |
| 81 | + |
| 82 | + /* Restore original GNUPGHOME environment variable */ |
| 83 | + if (g_session.gnupghome_saved) { |
| 84 | + if (g_session.had_original_gnupghome) { |
| 85 | + log_debug("Restoring original GNUPGHOME: %s", g_session.original_gnupghome); |
| 86 | + setenv("GNUPGHOME", g_session.original_gnupghome, 1); |
| 87 | + } else { |
| 88 | + log_debug("Unsetting GNUPGHOME (was not set originally)"); |
| 89 | + unsetenv("GNUPGHOME"); |
| 90 | + } |
| 91 | + g_session.gnupghome_saved = false; |
| 92 | + } |
| 93 | + |
| 94 | + /* Clear session state */ |
| 95 | + memset(&g_session, 0, sizeof(g_session)); |
| 96 | + log_debug("Session cleanup complete"); |
| 97 | +} |
| 98 | + |
| 44 | 99 | /* Switch to specified account with SSH isolation and validation */ |
| 45 | 100 | int accounts_switch(gitswitch_ctx_t *ctx, const char *identifier) { |
| 46 | 101 | account_t *account; |
@@ -66,6 +121,21 @@ int accounts_switch(gitswitch_ctx_t *ctx, const char *identifier) { |
| 66 | 121 | return -1; |
| 67 | 122 | } |
| 68 | 123 | |
| 124 | + /* Clean up any previous session before starting new one */ |
| 125 | + accounts_session_cleanup(); |
| 126 | + |
| 127 | + /* Save original GNUPGHOME if not already saved */ |
| 128 | + if (!g_session.gnupghome_saved) { |
| 129 | + const char *orig = getenv("GNUPGHOME"); |
| 130 | + if (orig) { |
| 131 | + safe_strncpy(g_session.original_gnupghome, orig, sizeof(g_session.original_gnupghome)); |
| 132 | + g_session.had_original_gnupghome = true; |
| 133 | + } else { |
| 134 | + g_session.had_original_gnupghome = false; |
| 135 | + } |
| 136 | + g_session.gnupghome_saved = true; |
| 137 | + } |
| 138 | + |
| 69 | 139 | /* Determine git scope - use account preference or context default */ |
| 70 | 140 | git_scope_t scope = account->preferred_scope; |
| 71 | 141 | if (scope == GIT_SCOPE_LOCAL && !git_is_repository()) { |
@@ -105,20 +175,21 @@ int accounts_switch(gitswitch_ctx_t *ctx, const char *identifier) { |
| 105 | 175 | if (account->ssh_enabled && strlen(account->ssh_key_path) > 0) { |
| 106 | 176 | log_info("Setting up SSH isolation for account: %s", account->name); |
| 107 | 177 | |
| 108 | | - /* Initialize SSH manager with isolated agents */ |
| 109 | | - ssh_config_t ssh_config = {0}; |
| 110 | | - if (ssh_manager_init(&ssh_config, SSH_AGENT_ISOLATED) != 0) { |
| 178 | + /* Initialize SSH manager with isolated agents using session state */ |
| 179 | + memset(&g_session.ssh_config, 0, sizeof(g_session.ssh_config)); |
| 180 | + if (ssh_manager_init(&g_session.ssh_config, SSH_AGENT_ISOLATED) != 0) { |
| 111 | 181 | printf(" [!!] SSH agent failed to start\n"); |
| 112 | 182 | log_warning("Failed to initialize SSH manager: %s", get_last_error()->message); |
| 113 | 183 | } else { |
| 114 | 184 | /* Switch to account's SSH configuration */ |
| 115 | | - if (ssh_switch_account(&ssh_config, account) != 0) { |
| 185 | + if (ssh_switch_account(&g_session.ssh_config, account) != 0) { |
| 116 | 186 | printf(" [!!] SSH key failed to load\n"); |
| 117 | 187 | log_warning("Failed to switch SSH configuration: %s", get_last_error()->message); |
| 118 | 188 | /* Clean up SSH manager on failure */ |
| 119 | | - ssh_manager_cleanup(&ssh_config); |
| 189 | + ssh_manager_cleanup(&g_session.ssh_config); |
| 120 | 190 | } else { |
| 121 | 191 | ssh_ok = true; |
| 192 | + g_session.ssh_active = true; /* Mark session as active for cleanup */ |
| 122 | 193 | printf(" [OK] SSH key loaded\n"); |
| 123 | 194 | log_info("SSH isolation activated for account: %s", account->name); |
| 124 | 195 | |
@@ -144,23 +215,24 @@ int accounts_switch(gitswitch_ctx_t *ctx, const char *identifier) { |
| 144 | 215 | if (account->gpg_enabled && strlen(account->gpg_key_id) > 0) { |
| 145 | 216 | log_info("Setting up GPG isolation for account: %s", account->name); |
| 146 | 217 | |
| 147 | | - /* Initialize GPG manager with isolated environments */ |
| 148 | | - gpg_config_t gpg_config = {0}; |
| 149 | | - if (gpg_manager_init(&gpg_config, GPG_MODE_ISOLATED) != 0) { |
| 218 | + /* Initialize GPG manager with isolated environments using session state */ |
| 219 | + memset(&g_session.gpg_config, 0, sizeof(g_session.gpg_config)); |
| 220 | + if (gpg_manager_init(&g_session.gpg_config, GPG_MODE_ISOLATED) != 0) { |
| 150 | 221 | printf(" [!!] GPG manager failed to initialize\n"); |
| 151 | 222 | log_warning("Failed to initialize GPG manager: %s", get_last_error()->message); |
| 152 | 223 | } else { |
| 153 | 224 | /* Switch to account's GPG configuration */ |
| 154 | | - if (gpg_switch_account(&gpg_config, account) != 0) { |
| 225 | + if (gpg_switch_account(&g_session.gpg_config, account) != 0) { |
| 155 | 226 | printf(" [!!] GPG key failed to activate\n"); |
| 156 | 227 | log_warning("Failed to switch GPG configuration: %s", get_last_error()->message); |
| 157 | 228 | /* Clean up GPG manager on failure */ |
| 158 | | - gpg_manager_cleanup(&gpg_config); |
| 229 | + gpg_manager_cleanup(&g_session.gpg_config); |
| 159 | 230 | } else { |
| 231 | + g_session.gpg_active = true; /* Mark session as active for cleanup */ |
| 160 | 232 | log_info("GPG isolation activated for account: %s", account->name); |
| 161 | 233 | |
| 162 | 234 | /* Configure git GPG signing */ |
| 163 | | - if (gpg_configure_git_signing(&gpg_config, account, scope) != 0) { |
| 235 | + if (gpg_configure_git_signing(&g_session.gpg_config, account, scope) != 0) { |
| 164 | 236 | printf(" [!!] GPG signing config failed\n"); |
| 165 | 237 | log_warning("Failed to configure git GPG signing: %s", get_last_error()->message); |
| 166 | 238 | } else { |
@@ -198,6 +270,15 @@ int accounts_switch(gitswitch_ctx_t *ctx, const char *identifier) { |
| 198 | 270 | /* Set as current account */ |
| 199 | 271 | ctx->current_account = account; |
| 200 | 272 | |
| 273 | + /* Print shell integration tip if SSH was set up */ |
| 274 | + if (ssh_ok) { |
| 275 | + const char *runtime_dir = getenv("XDG_RUNTIME_DIR"); |
| 276 | + if (runtime_dir) { |
| 277 | + printf("\n Tip: Add to your shell rc for persistent SSH:\n"); |
| 278 | + printf(" export SSH_AUTH_SOCK=%s/gitswitch-ssh/current.sock\n", runtime_dir); |
| 279 | + } |
| 280 | + } |
| 281 | + |
| 201 | 282 | printf("\n"); |
| 202 | 283 | log_info("Successfully switched to account: %s (%s)", account->name, account->description); |
| 203 | 284 | return 0; |