From 0750dbc8a77459ec4f89967ab5340922a8ea3446 Mon Sep 17 00:00:00 2001 From: jesperpedersen Date: Tue, 16 Mar 2021 09:10:25 -0400 Subject: [PATCH] [#143] Fix segfault for missing home directory --- doc/man/pgagroal-admin.1.rst | 2 +- src/admin.c | 16 ++++++++++++++++ src/include/utils.h | 7 +++++++ src/libpgagroal/utils.c | 18 ++++++++++++++++++ 4 files changed, 42 insertions(+), 1 deletion(-) diff --git a/doc/man/pgagroal-admin.1.rst b/doc/man/pgagroal-admin.1.rst index 47e66582..179f5c24 100644 --- a/doc/man/pgagroal-admin.1.rst +++ b/doc/man/pgagroal-admin.1.rst @@ -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 diff --git a/src/admin.c b/src/admin.c index 2baaf38e..b08b40b7 100644 --- a/src/admin.c +++ b/src/admin.c @@ -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()); diff --git a/src/include/utils.h b/src/include/utils.h index 0117b83e..848ce54c 100644 --- a/src/include/utils.h +++ b/src/include/utils.h @@ -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 diff --git a/src/libpgagroal/utils.c b/src/libpgagroal/utils.c index b9b7ae4f..d8ce78eb 100644 --- a/src/libpgagroal/utils.c +++ b/src/libpgagroal/utils.c @@ -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) {