Skip to content

Commit

Permalink
Added multiple error types for master key
Browse files Browse the repository at this point in the history
  • Loading branch information
palak-chaturvedi committed Apr 27, 2024
1 parent 6eeff18 commit d0c16c3
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 10 deletions.
77 changes: 73 additions & 4 deletions src/admin.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ int
main(int argc, char** argv)
{
int c;
int error_code;
char* masterkey = NULL;
char* username = NULL;
char* password = NULL;
char* file_path = NULL;
Expand Down Expand Up @@ -285,6 +287,35 @@ main(int argc, char** argv)
file_path = PGAGROAL_DEFAULT_USERS_FILE;
}

if(master_key_path != NULL)
{
error_code = pgagroal_get_master_key(&masterkey, master_key_path);

if (error_code)
{
if(error_code == HOME_DIRECTORY_ERROR)
{
warnx("home directory not found");
}

else if(error_code == PGAGROAL_DIRECTORY_ERROR)
{
warnx(".pgagroal not found");
}
else if(error_code == FILE_NOT_FOUND_ERROR)
{
warnx("Invalid master key file location");
}
else
{
warnx("Invalid master key");
}
goto error;
}
}



if (parsed.cmd->action == ACTION_MASTER_KEY)
{
if (master_key(password, generate_pwd, pwd_length, master_key_path))
Expand Down Expand Up @@ -509,10 +540,29 @@ add_user(char* users_path, char* username, char* password, bool generate_pwd, in
bool do_verify = true;
char* verify = NULL;
bool do_free = true;
int error_code;

error_code = pgagroal_get_master_key(&master_key, filename);

if (pgagroal_get_master_key(&master_key, filename))
if (error_code)
{
warnx("Invalid master key");
if(error_code == HOME_DIRECTORY_ERROR)
{
warnx("home directory not found");
}

else if(error_code == PGAGROAL_DIRECTORY_ERROR)
{
warnx(".pgagroal not found");
}
else if(error_code == FILE_NOT_FOUND_ERROR)
{
warnx("Invalid master key file location");
}
else
{
warnx("Invalid master key");
}
goto error;
}

Expand Down Expand Up @@ -687,12 +737,31 @@ update_user(char* users_path, char* username, char* password, bool generate_pwd,
bool do_verify = true;
char* verify = NULL;
bool do_free = true;
int error_code;

memset(&tmpfilename, 0, sizeof(tmpfilename));

error_code = pgagroal_get_master_key(&master_key, filename);

if (pgagroal_get_master_key(&master_key, filename))
if (error_code)
{
warnx("Invalid master key");
if(error_code == HOME_DIRECTORY_ERROR)
{
warnx("home directory not found");
}

else if(error_code == PGAGROAL_DIRECTORY_ERROR)
{
warnx(".pgagroal not found");
}
else if(error_code == FILE_NOT_FOUND_ERROR)
{
warnx("Invalid master key file location");
}
else
{
warnx("Invalid master key");
}
goto error;
}

Expand Down
5 changes: 5 additions & 0 deletions src/include/security.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ extern "C" {

#include <openssl/ssl.h>

#define HOME_DIRECTORY_ERROR 1
#define PGAGROAL_DIRECTORY_ERROR 2
#define FILE_NOT_FOUND_ERROR 3
#define INVALID_MASTERKEY_ERROR 4

/**
* Authenticate a user
* @param client_fd The descriptor
Expand Down
40 changes: 34 additions & 6 deletions src/libpgagroal/security.c
Original file line number Diff line number Diff line change
Expand Up @@ -3183,15 +3183,15 @@ pgagroal_get_master_key(char** masterkey, char* filename)
{
if (pgagroal_get_home_directory() == NULL)
{
goto error;
goto home_directory_error;
}

memset(&buf, 0, sizeof(buf));
snprintf(&buf[0], sizeof(buf), "%s/.pgagroal", pgagroal_get_home_directory());

if (stat(&buf[0], &st) == -1)
{
goto error;
goto pgagroal_directory_error;
}
else
{
Expand All @@ -3201,7 +3201,7 @@ pgagroal_get_master_key(char** masterkey, char* filename)
}
else
{
goto error;
goto file_not_found;
}
}

Expand All @@ -3210,7 +3210,7 @@ pgagroal_get_master_key(char** masterkey, char* filename)

if (stat(&buf[0], &st) == -1)
{
goto error;
goto file_not_found;
}
else
{
Expand All @@ -3234,7 +3234,7 @@ pgagroal_get_master_key(char** masterkey, char* filename)

if (master_key_file == NULL)
{
goto error;
goto file_not_found;
}

memset(&line, 0, sizeof(line));
Expand All @@ -3260,7 +3260,35 @@ pgagroal_get_master_key(char** masterkey, char* filename)
fclose(master_key_file);
}

return 1;
return INVALID_MASTERKEY_ERROR;

file_not_found:

free(mk);
if (master_key_file)
{
fclose(master_key_file);
}

return FILE_NOT_FOUND_ERROR;

pgagroal_directory_error:
free(mk);
if (master_key_file)
{
fclose(master_key_file);
}

return PGAGROAL_DIRECTORY_ERROR;

home_directory_error:
free(mk);
if (master_key_file)
{
fclose(master_key_file);
}

return HOME_DIRECTORY_ERROR;
}

int
Expand Down

0 comments on commit d0c16c3

Please sign in to comment.