Skip to content

Commit

Permalink
[#143] Fix segfault for missing home directory
Browse files Browse the repository at this point in the history
  • Loading branch information
jesperpedersen committed Mar 16, 2021
1 parent e1531d4 commit 0750dbc
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion doc/man/pgagroal-admin.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ COMMANDS
========

master-key
Create or update the master key
Create or update the master key. The master key will be created in the pgagroal user home directory under ~/.pgagroal

add-user
Add a user
Expand Down
16 changes: 16 additions & 0 deletions src/admin.c
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,22 @@ master_key(char* password, bool generate_pwd, int pwd_length)
struct stat st = {0};
bool do_free = true;

if (pgagroal_get_home_directory() == NULL)
{
char* username = pgagroal_get_user_name();

if (username != NULL)
{
printf("No home directory for user \'%s\'\n", username);
}
else
{
printf("No home directory for user running pgagroal\n");
}

goto error;
}

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

Expand Down
7 changes: 7 additions & 0 deletions src/include/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,13 @@ pgagroal_libev_engine(unsigned int val);
char*
pgagroal_get_home_directory(void);

/**
* Get the user name
* @return The user name
*/
char*
pgagroal_get_user_name(void);

/**
* Get a password from stdin
* @return The password
Expand Down
18 changes: 18 additions & 0 deletions src/libpgagroal/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -557,9 +557,27 @@ pgagroal_get_home_directory(void)
{
struct passwd *pw = getpwuid(getuid());

if (pw == NULL)
{
return NULL;
}

return pw->pw_dir;
}

char*
pgagroal_get_user_name(void)
{
struct passwd *pw = getpwuid(getuid());

if (pw == NULL)
{
return NULL;
}

return pw->pw_name;
}

char*
pgagroal_get_password(void)
{
Expand Down

0 comments on commit 0750dbc

Please sign in to comment.