@@ -2,6 +2,9 @@ |
| 2 | * Implements secure account switching and management for gitswitch-c | 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 | #include <stdio.h> | 8 | #include <stdio.h> |
| 6 | #include <stdlib.h> | 9 | #include <stdlib.h> |
| 7 | #include <string.h> | 10 | #include <string.h> |
@@ -18,6 +21,20 @@ |
| 18 | #include "ssh_manager.h" | 21 | #include "ssh_manager.h" |
| 19 | #include "gpg_manager.h" | 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 | /* Internal helper functions */ | 38 | /* Internal helper functions */ |
| 22 | static uint32_t get_next_available_id(const gitswitch_ctx_t *ctx); | 39 | static uint32_t get_next_available_id(const gitswitch_ctx_t *ctx); |
| 23 | static int validate_ssh_key_security(const char *ssh_key_path); | 40 | static int validate_ssh_key_security(const char *ssh_key_path); |
@@ -31,16 +48,54 @@ int accounts_init(gitswitch_ctx_t *ctx) { |
| 31 | set_error(ERR_INVALID_ARGS, "NULL context to accounts_init"); | 48 | set_error(ERR_INVALID_ARGS, "NULL context to accounts_init"); |
| 32 | return -1; | 49 | return -1; |
| 33 | } | 50 | } |
| 34 | - | 51 | + |
| 35 | /* Initialize account array */ | 52 | /* Initialize account array */ |
| 36 | memset(ctx->accounts, 0, sizeof(ctx->accounts)); | 53 | memset(ctx->accounts, 0, sizeof(ctx->accounts)); |
| 37 | ctx->account_count = 0; | 54 | ctx->account_count = 0; |
| 38 | ctx->current_account = NULL; | 55 | ctx->current_account = NULL; |
| 39 | - | 56 | + |
| | 57 | + /* Initialize session state */ |
| | 58 | + memset(&g_session, 0, sizeof(g_session)); |
| | 59 | + |
| 40 | log_debug("Accounts system initialized"); | 60 | log_debug("Accounts system initialized"); |
| 41 | return 0; | 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 | /* Switch to specified account with SSH isolation and validation */ | 99 | /* Switch to specified account with SSH isolation and validation */ |
| 45 | int accounts_switch(gitswitch_ctx_t *ctx, const char *identifier) { | 100 | int accounts_switch(gitswitch_ctx_t *ctx, const char *identifier) { |
| 46 | account_t *account; | 101 | account_t *account; |
@@ -66,6 +121,21 @@ int accounts_switch(gitswitch_ctx_t *ctx, const char *identifier) { |
| 66 | return -1; | 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 | /* Determine git scope - use account preference or context default */ | 139 | /* Determine git scope - use account preference or context default */ |
| 70 | git_scope_t scope = account->preferred_scope; | 140 | git_scope_t scope = account->preferred_scope; |
| 71 | if (scope == GIT_SCOPE_LOCAL && !git_is_repository()) { | 141 | if (scope == GIT_SCOPE_LOCAL && !git_is_repository()) { |
@@ -105,20 +175,21 @@ int accounts_switch(gitswitch_ctx_t *ctx, const char *identifier) { |
| 105 | if (account->ssh_enabled && strlen(account->ssh_key_path) > 0) { | 175 | if (account->ssh_enabled && strlen(account->ssh_key_path) > 0) { |
| 106 | log_info("Setting up SSH isolation for account: %s", account->name); | 176 | log_info("Setting up SSH isolation for account: %s", account->name); |
| 107 | | 177 | |
| 108 | - /* Initialize SSH manager with isolated agents */ | 178 | + /* Initialize SSH manager with isolated agents using session state */ |
| 109 | - ssh_config_t ssh_config = {0}; | 179 | + memset(&g_session.ssh_config, 0, sizeof(g_session.ssh_config)); |
| 110 | - if (ssh_manager_init(&ssh_config, SSH_AGENT_ISOLATED) != 0) { | 180 | + if (ssh_manager_init(&g_session.ssh_config, SSH_AGENT_ISOLATED) != 0) { |
| 111 | printf(" [!!] SSH agent failed to start\n"); | 181 | printf(" [!!] SSH agent failed to start\n"); |
| 112 | log_warning("Failed to initialize SSH manager: %s", get_last_error()->message); | 182 | log_warning("Failed to initialize SSH manager: %s", get_last_error()->message); |
| 113 | } else { | 183 | } else { |
| 114 | /* Switch to account's SSH configuration */ | 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 | printf(" [!!] SSH key failed to load\n"); | 186 | printf(" [!!] SSH key failed to load\n"); |
| 117 | log_warning("Failed to switch SSH configuration: %s", get_last_error()->message); | 187 | log_warning("Failed to switch SSH configuration: %s", get_last_error()->message); |
| 118 | /* Clean up SSH manager on failure */ | 188 | /* Clean up SSH manager on failure */ |
| 119 | - ssh_manager_cleanup(&ssh_config); | 189 | + ssh_manager_cleanup(&g_session.ssh_config); |
| 120 | } else { | 190 | } else { |
| 121 | ssh_ok = true; | 191 | ssh_ok = true; |
| | 192 | + g_session.ssh_active = true; /* Mark session as active for cleanup */ |
| 122 | printf(" [OK] SSH key loaded\n"); | 193 | printf(" [OK] SSH key loaded\n"); |
| 123 | log_info("SSH isolation activated for account: %s", account->name); | 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 | if (account->gpg_enabled && strlen(account->gpg_key_id) > 0) { | 215 | if (account->gpg_enabled && strlen(account->gpg_key_id) > 0) { |
| 145 | log_info("Setting up GPG isolation for account: %s", account->name); | 216 | log_info("Setting up GPG isolation for account: %s", account->name); |
| 146 | | 217 | |
| 147 | - /* Initialize GPG manager with isolated environments */ | 218 | + /* Initialize GPG manager with isolated environments using session state */ |
| 148 | - gpg_config_t gpg_config = {0}; | 219 | + memset(&g_session.gpg_config, 0, sizeof(g_session.gpg_config)); |
| 149 | - if (gpg_manager_init(&gpg_config, GPG_MODE_ISOLATED) != 0) { | 220 | + if (gpg_manager_init(&g_session.gpg_config, GPG_MODE_ISOLATED) != 0) { |
| 150 | printf(" [!!] GPG manager failed to initialize\n"); | 221 | printf(" [!!] GPG manager failed to initialize\n"); |
| 151 | log_warning("Failed to initialize GPG manager: %s", get_last_error()->message); | 222 | log_warning("Failed to initialize GPG manager: %s", get_last_error()->message); |
| 152 | } else { | 223 | } else { |
| 153 | /* Switch to account's GPG configuration */ | 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 | printf(" [!!] GPG key failed to activate\n"); | 226 | printf(" [!!] GPG key failed to activate\n"); |
| 156 | log_warning("Failed to switch GPG configuration: %s", get_last_error()->message); | 227 | log_warning("Failed to switch GPG configuration: %s", get_last_error()->message); |
| 157 | /* Clean up GPG manager on failure */ | 228 | /* Clean up GPG manager on failure */ |
| 158 | - gpg_manager_cleanup(&gpg_config); | 229 | + gpg_manager_cleanup(&g_session.gpg_config); |
| 159 | } else { | 230 | } else { |
| | 231 | + g_session.gpg_active = true; /* Mark session as active for cleanup */ |
| 160 | log_info("GPG isolation activated for account: %s", account->name); | 232 | log_info("GPG isolation activated for account: %s", account->name); |
| 161 | | 233 | |
| 162 | /* Configure git GPG signing */ | 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 | printf(" [!!] GPG signing config failed\n"); | 236 | printf(" [!!] GPG signing config failed\n"); |
| 165 | log_warning("Failed to configure git GPG signing: %s", get_last_error()->message); | 237 | log_warning("Failed to configure git GPG signing: %s", get_last_error()->message); |
| 166 | } else { | 238 | } else { |
@@ -198,6 +270,15 @@ int accounts_switch(gitswitch_ctx_t *ctx, const char *identifier) { |
| 198 | /* Set as current account */ | 270 | /* Set as current account */ |
| 199 | ctx->current_account = account; | 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 | printf("\n"); | 282 | printf("\n"); |
| 202 | log_info("Successfully switched to account: %s (%s)", account->name, account->description); | 283 | log_info("Successfully switched to account: %s (%s)", account->name, account->description); |
| 203 | return 0; | 284 | return 0; |