Skip to content

Commit

Permalink
Fix username lookup in case of missing USER environment variable
Browse files Browse the repository at this point in the history
  • Loading branch information
DariuszOstolski committed Sep 19, 2017
1 parent 2a6df66 commit 2d3cf26
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
26 changes: 25 additions & 1 deletion src/utilities.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@
#ifdef HAVE_SYSLOG_H
# include <syslog.h>
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h> // For geteuid.
#endif
#ifdef HAVE_PWD_H
# include <pwd.h>
#endif

#include "base/googleinit.h"

Expand Down Expand Up @@ -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());

Expand Down

0 comments on commit 2d3cf26

Please sign in to comment.