Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix username lookup in case of missing USER environment variable #245

Merged
merged 2 commits into from
Oct 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a whitespace right before "result"?

struct passwd* result = NULL

char buffer[1024] = {'\0'};
uid = geteuid();
int pwuid_res = getpwuid_r(uid, &pwd, buffer, sizeof (buffer), &result);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you remove the whitespace between sizeof and buffer?

if(pwuid_res == 0) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A whitespace after "if"

g_my_user_name = pwd.pw_name;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please concatenate with the next line

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