Skip to content

Commit b314286

Browse files
authored
res_phreaknet: Create temp keypair files using full path. (#57)
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
1 parent 6198b9a commit b314286

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

res/res_phreaknet.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,8 @@ static int iax_purge_keys(void)
10331033
return 0;
10341034
}
10351035

1036+
#define MY_KEYPAIR_TEMPFILE_PREFIX "/tmp/" MY_KEYPAIR_NAME
1037+
10361038
/*! \brief Generate a new or rotated PhreakNet public key pair and upload it to the server */
10371039
static int gen_keypair(int rotate)
10381040
{
@@ -1044,7 +1046,8 @@ static int gen_keypair(int rotate)
10441046
char *pubkey;
10451047
struct ast_str *str;
10461048
const char *clli;
1047-
char *const argv[] = { "astgenkey", "-q", "-n", MY_KEYPAIR_NAME, NULL };
1049+
struct stat st;
1050+
char *const argv[] = { "astgenkey", "-q", "-n", MY_KEYPAIR_TEMPFILE_PREFIX, NULL };
10481051

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

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

10991106
/* astgenkey will create files in the current working directory, not ast_config_AST_KEY_DIR.
11001107
* 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. */
1101-
if (rename(MY_KEYPAIR_NAME ".key", privkeyfile)) {
1102-
ast_log(LOG_WARNING, "Failed to rename file to %s: %s\n", privkeyfile, strerror(errno));
1108+
if (rename(MY_KEYPAIR_TEMPFILE_PREFIX ".key", privkeyfile)) {
1109+
ast_log(LOG_ERROR, "Failed to rename %s to %s: %s\n", MY_KEYPAIR_TEMPFILE_PREFIX ".key", privkeyfile, strerror(errno));
11031110
return -1;
1104-
} else if (rename(MY_KEYPAIR_NAME ".pub", pubkeyfile)) {
1105-
ast_log(LOG_WARNING, "Failed to rename file to %s: %s\n", pubkeyfile, strerror(errno));
1111+
} else if (rename(MY_KEYPAIR_TEMPFILE_PREFIX ".pub", pubkeyfile)) {
1112+
ast_log(LOG_ERROR, "Failed to rename %s to %s: %s\n", MY_KEYPAIR_TEMPFILE_PREFIX ".pub", pubkeyfile, strerror(errno));
11061113
return -1;
11071114
}
11081115

0 commit comments

Comments
 (0)