Skip to content

Commit

Permalink
res_phreaknet: Create temp keypair files using full path. (#57)
Browse files Browse the repository at this point in the history
Rather than creating the new keypair files in the current
working directory temporarily and then moving them to the
permanent directory, use a full path (specifically within
/tmp) to create the files, to avoid permissions errors
if we can't write to the current working directory.

Resolves: #55
  • Loading branch information
InterLinked1 authored Dec 22, 2024
1 parent 6198b9a commit b314286
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions res/res_phreaknet.c
Original file line number Diff line number Diff line change
Expand Up @@ -1033,6 +1033,8 @@ static int iax_purge_keys(void)
return 0;
}

#define MY_KEYPAIR_TEMPFILE_PREFIX "/tmp/" MY_KEYPAIR_NAME

/*! \brief Generate a new or rotated PhreakNet public key pair and upload it to the server */
static int gen_keypair(int rotate)
{
Expand All @@ -1044,7 +1046,8 @@ static int gen_keypair(int rotate)
char *pubkey;
struct ast_str *str;
const char *clli;
char *const argv[] = { "astgenkey", "-q", "-n", MY_KEYPAIR_NAME, NULL };
struct stat st;
char *const argv[] = { "astgenkey", "-q", "-n", MY_KEYPAIR_TEMPFILE_PREFIX, NULL };

if (ast_strlen_zero(interlinked_api_key)) {
/* Can't successfully upload to server with no API key */
Expand Down Expand Up @@ -1091,18 +1094,22 @@ static int gen_keypair(int rotate)
}

/* Generate the new keypair, and wait for it to finish. */
ast_debug(3, "Executing: astgenkey -q -n %s\n", MY_KEYPAIR_TEMPFILE_PREFIX);
if (ast_safe_execvp(0, "astgenkey", argv) == -1) {
ast_log(LOG_WARNING, "Failed to execute astgenkey: %s\n", strerror(errno));
return -1;
} else if (stat(MY_KEYPAIR_TEMPFILE_PREFIX ".key", &st)) {
ast_log(LOG_WARNING, "Failed to stat %s: %s\n", MY_KEYPAIR_TEMPFILE_PREFIX ".key", strerror(errno));
return -1;
}

/* astgenkey will create files in the current working directory, not ast_config_AST_KEY_DIR.
* We could chdir after we fork but before we execvp, but let's just rename the file from what it is to what it should be. */
if (rename(MY_KEYPAIR_NAME ".key", privkeyfile)) {
ast_log(LOG_WARNING, "Failed to rename file to %s: %s\n", privkeyfile, strerror(errno));
if (rename(MY_KEYPAIR_TEMPFILE_PREFIX ".key", privkeyfile)) {
ast_log(LOG_ERROR, "Failed to rename %s to %s: %s\n", MY_KEYPAIR_TEMPFILE_PREFIX ".key", privkeyfile, strerror(errno));
return -1;
} else if (rename(MY_KEYPAIR_NAME ".pub", pubkeyfile)) {
ast_log(LOG_WARNING, "Failed to rename file to %s: %s\n", pubkeyfile, strerror(errno));
} else if (rename(MY_KEYPAIR_TEMPFILE_PREFIX ".pub", pubkeyfile)) {
ast_log(LOG_ERROR, "Failed to rename %s to %s: %s\n", MY_KEYPAIR_TEMPFILE_PREFIX ".pub", pubkeyfile, strerror(errno));
return -1;
}

Expand Down

0 comments on commit b314286

Please sign in to comment.