From bdc37ce98e550e487ff284f050d8bc10b2587736 Mon Sep 17 00:00:00 2001 From: InterLinked1 <24227567+InterLinked1@users.noreply.github.com> Date: Thu, 19 Dec 2024 17:31:58 -0500 Subject: [PATCH] res_phreaknet: Create temp keypair files using full path. 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 --- res/res_phreaknet.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/res/res_phreaknet.c b/res/res_phreaknet.c index 5ac24ac..ca38a46 100644 --- a/res/res_phreaknet.c +++ b/res/res_phreaknet.c @@ -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) { @@ -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 */ @@ -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; }