diff --git a/src/admin.c b/src/admin.c index c1b94ccc..cc1f44ab 100644 --- a/src/admin.c +++ b/src/admin.c @@ -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 */ diff --git a/src/include/utils.h b/src/include/utils.h index 7c377186..3a2b10b5 100644 --- a/src/include/utils.h +++ b/src/include/utils.h @@ -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 diff --git a/src/libpgagroal/utils.c b/src/libpgagroal/utils.c index 040ad2d4..e7036668 100644 --- a/src/libpgagroal/utils.c +++ b/src/libpgagroal/utils.c @@ -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) {