Skip to content

Commit

Permalink
[#425] Force master-key to abort if file exist
Browse files Browse the repository at this point in the history
  • Loading branch information
jesperpedersen committed Mar 26, 2024
1 parent 8a1d641 commit 2126075
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/admin.c
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,12 @@ master_key(char* password, bool generate_pwd, int pwd_length)
memset(&buf, 0, sizeof(buf));
snprintf(&buf[0], sizeof(buf), "%s/.pgagroal/master.key", pgagroal_get_home_directory());

if (pgagroal_exists(&buf[0]))
{
warnx("The file ~/.pgexporter/master.key already exists");
goto error;
}

if (stat(&buf[0], &st) == -1)
{
/* Ok */
Expand Down
8 changes: 8 additions & 0 deletions src/include/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,14 @@ pgagroal_get_user_name(void);
char*
pgagroal_get_password(void);

/**
* File/directory exists
* @param f The file/directory
* @return The result
*/
bool
pgagroal_exists(char* f);

/**
* BASE64 encode a string
* @param raw The string
Expand Down
11 changes: 11 additions & 0 deletions src/libpgagroal/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,17 @@ pgagroal_get_password(void)
return result;
}

bool
pgagroal_exists(char* f)
{
if (access(f, F_OK) == 0)
{
return true;
}

return false;
}

int
pgagroal_base64_encode(char* raw, int raw_length, char** encoded)
{
Expand Down

0 comments on commit 2126075

Please sign in to comment.