Skip to content

res_phreaknet: Create temp keypair files using full path. #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading