From 2d3cf2681d4bd4019af368d549395fe2c00a0154 Mon Sep 17 00:00:00 2001 From: Dariusz Ostolski Date: Tue, 19 Sep 2017 22:23:53 +0200 Subject: [PATCH 1/2] Fix username lookup in case of missing USER environment variable --- configure.ac | 1 + src/utilities.cc | 26 +++++++++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/configure.ac b/configure.ac index 69711b746..6a19cd0b2 100644 --- a/configure.ac +++ b/configure.ac @@ -31,6 +31,7 @@ AC_HEADER_STDC AC_CHECK_HEADER(stdint.h, ac_cv_have_stdint_h=1, ac_cv_have_stdint_h=0) AC_CHECK_HEADER(sys/types.h, ac_cv_have_systypes_h=1, ac_cv_have_systypes_h=0) AC_CHECK_HEADER(inttypes.h, ac_cv_have_inttypes_h=1, ac_cv_have_inttypes_h=0) +AC_CHECK_HEADER(pwd.h, ac_cv_have_pwd_h=1, ac_cv_have_pwd_h=0) AC_CHECK_HEADERS(unistd.h, ac_cv_have_unistd_h=1, ac_cv_have_unistd_h=0) AC_CHECK_HEADERS(syscall.h) AC_CHECK_HEADERS(sys/syscall.h) diff --git a/src/utilities.cc b/src/utilities.cc index 9d5b1239b..1a3c7e1f8 100644 --- a/src/utilities.cc +++ b/src/utilities.cc @@ -47,6 +47,12 @@ #ifdef HAVE_SYSLOG_H # include #endif +#ifdef HAVE_UNISTD_H +# include // For geteuid. +#endif +#ifdef HAVE_PWD_H +# include +#endif #include "base/googleinit.h" @@ -299,8 +305,26 @@ static void MyUserNameInitializer() { if (user != NULL) { g_my_user_name = user; } else { - g_my_user_name = "invalid-user"; +#if defined(HAVE_PWD_H) && defined(HAVE_UNISTD_H) + uid_t uid; + struct passwd pwd; + struct passwd*result = NULL; + char buffer[1024] = {'\0'}; + uid = geteuid(); + int pwuid_res = getpwuid_r(uid, &pwd, buffer, sizeof (buffer), &result); + if(pwuid_res == 0) { + g_my_user_name = pwd.pw_name; + } + else { + snprintf(buffer, sizeof(buffer), "uid%d", uid); + g_my_user_name = buffer; + } +#endif + if(g_my_user_name.empty()) { + g_my_user_name = "invalid-user"; + } } + } REGISTER_MODULE_INITIALIZER(utilities, MyUserNameInitializer()); From 9d28cac4690e32eae94e180ea0d6d7ce0b6fc71d Mon Sep 17 00:00:00 2001 From: Dariusz Ostolski Date: Wed, 20 Sep 2017 18:41:07 +0200 Subject: [PATCH 2/2] Added HAVE_PWD_H to cmake build --- CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index b6d4644ee..061c182a8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -84,6 +84,7 @@ check_include_file (syslog.h HAVE_SYSLOG_H) check_include_file (ucontext.h HAVE_UCONTEXT_H) check_include_file (unistd.h HAVE_UNISTD_H) check_include_file (unwind.h HAVE_UNWIND_H) +check_include_file (pwd.h HAVE_PWD_H) check_include_file_cxx ("ext/hash_map" HAVE_EXT_HASH_MAP) check_include_file_cxx ("ext/hash_set" HAVE_EXT_HASH_SET)