ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
homedir.h File Reference

Temporary GPG homedir management for isolated key operations. More...

Go to the source code of this file.

Typedefs

typedef struct gpg_homedir gpg_homedir_t
 Opaque handle to a temporary GPG homedir.

Functions

gpg_homedir_t * gpg_homedir_create (void)
 Create a temporary GPG homedir for isolated key operations.
const char * gpg_homedir_path (const gpg_homedir_t *homedir)
 Get the homedir path for use in GPG commands.
void gpg_homedir_destroy (gpg_homedir_t *homedir)
 Destroy a temporary GPG homedir and clean up all files.

Detailed Description

Temporary GPG homedir management for isolated key operations.

Provides utilities for creating and managing temporary GPG homedirs to isolate key operations (import, decrypt, sign) without polluting the user's main GPG keyring.

Using a temporary homedir provides:

  • Isolation from user's keys (no risk of deleting wrong keys)
  • Automatic cleanup (just delete the directory)
  • Better error handling and race condition avoidance
  • Cleaner, more maintainable code

Example usage:

if (homedir == NULL) {
return SET_ERRNO(ERROR_CRYPTO_KEY, "Failed to create GPG homedir");
}
// Use homedir.path in gpg commands
char cmd[1024];
snprintf(cmd, sizeof(cmd), "gpg --homedir '%s' --batch --import '%s'",
homedir->path, key_file);
int status = system(cmd);
#define SET_ERRNO(code,...)
@ ERROR_CRYPTO_KEY
void gpg_homedir_destroy(gpg_homedir_t *homedir)
Destroy a temporary GPG homedir and clean up all files.
Definition homedir.c:62
gpg_homedir_t * gpg_homedir_create(void)
Create a temporary GPG homedir for isolated key operations.
Definition homedir.c:25
struct gpg_homedir gpg_homedir_t
Opaque handle to a temporary GPG homedir.
Definition homedir.h:45
char path[PLATFORM_MAX_PATH_LENGTH]
Definition homedir.c:22

Definition in file homedir.h.

Typedef Documentation

◆ gpg_homedir_t

typedef struct gpg_homedir gpg_homedir_t

Opaque handle to a temporary GPG homedir.

Definition at line 45 of file homedir.h.

Function Documentation

◆ gpg_homedir_create()

gpg_homedir_t * gpg_homedir_create ( void )

Create a temporary GPG homedir for isolated key operations.

Creates a new temporary directory configured for GPG use. The directory is isolated from the user's main GPG keyring.

Returns
Pointer to homedir handle on success, NULL on failure Caller must destroy with gpg_homedir_destroy()
Note
The directory is created in the system temp location (TMPDIR, /tmp, etc.)

Definition at line 25 of file homedir.c.

25 {
27 if (!homedir) {
28 log_error("Failed to allocate memory for GPG homedir handle");
29 return NULL;
30 }
31
32 /* Create temporary directory using platform abstraction */
33 if (platform_mkdtemp(homedir->path, sizeof(homedir->path),
34 "ascii-chat-gpg") != 0) {
35 log_error("Failed to create temporary GPG homedir");
36 return NULL;
37 }
38
39 defer_named(temp_directory, platform_rmdir_recursive(homedir->path));
40
41 /* Restrict permissions to owner only (mode 0700) using platform abstraction
42 */
43 if (platform_chmod(homedir->path, 0700) != 0) {
44 log_warn("Failed to set permissions on GPG homedir, attempting cleanup");
45 return NULL;
46 }
47
48 log_debug("Created temporary GPG homedir: %s", homedir->path);
49 defer_cancel(temp_directory);
50 gpg_homedir_t *result = homedir;
51 homedir = NULL;
52 return result;
53}
#define SAFE_MALLOC_DEFER_FREE(name, size, cast)
#define log_warn(...)
Log a WARN message.
Definition log/log.h:591
#define log_error(...)
Log an ERROR message.
Definition log/log.h:604
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:565

References log_debug, log_error, log_warn, and SAFE_MALLOC_DEFER_FREE.

◆ gpg_homedir_destroy()

void gpg_homedir_destroy ( gpg_homedir_t * homedir)

Destroy a temporary GPG homedir and clean up all files.

Recursively deletes the temporary directory and all its contents. Safe to call on NULL pointer (no-op).

Parameters
homedirHandle from gpg_homedir_create() (can be NULL)

Definition at line 62 of file homedir.c.

62 {
63 if (!homedir) {
64 return;
65 }
66
67 /* Recursively delete the entire directory and all contents using platform
68 * abstraction */
69 if (platform_rmdir_recursive(homedir->path) != 0) {
70 log_warn("Failed to completely clean up GPG homedir: %s", homedir->path);
71 } else {
72 log_debug("Cleaned up temporary GPG homedir: %s", homedir->path);
73 }
74
75 SAFE_FREE(homedir);
76}
#define SAFE_FREE(ptr)

References log_debug, log_warn, gpg_homedir::path, and SAFE_FREE.

◆ gpg_homedir_path()

const char * gpg_homedir_path ( const gpg_homedir_t * homedir)

Get the homedir path for use in GPG commands.

Returns the filesystem path of the temporary homedir. This path should be used with gpg's –homedir flag.

Parameters
homedirHandle from gpg_homedir_create()
Returns
Pointer to path string (valid until gpg_homedir_destroy() is called) Returns NULL if homedir is invalid

Definition at line 55 of file homedir.c.

55 {
56 if (!homedir) {
57 return NULL;
58 }
59 return homedir->path;
60}

References gpg_homedir::path.