roken: Handle not having getpwnam_r()

This commit is contained in:
Nicolas Williams
2022-01-13 18:31:01 -06:00
parent f06b7d1b38
commit 4d8badc9a8

View File

@@ -53,12 +53,12 @@ roken_get_shell(char *shell, size_t shellsz)
char *p; char *p;
#ifndef WIN32 #ifndef WIN32
char user[128]; #ifdef HAVE_GETPWNAM_R
const char *username = roken_get_username(user, sizeof(user));
size_t buflen = 2048; size_t buflen = 2048;
if (sysconf(_SC_GETPW_R_SIZE_MAX) > 0) if (sysconf(_SC_GETPW_R_SIZE_MAX) > 0)
buflen = sysconf(_SC_GETPW_R_SIZE_MAX); buflen = sysconf(_SC_GETPW_R_SIZE_MAX);
#endif
if (issuid()) if (issuid())
return "/bin/sh"; return "/bin/sh";
@@ -76,8 +76,11 @@ roken_get_shell(char *shell, size_t shellsz)
struct passwd pwd; struct passwd pwd;
struct passwd *pwdp; struct passwd *pwdp;
char buf[buflen]; char buf[buflen];
char user[128];
const char *username = roken_get_username(user, sizeof(user));
if (getpwnam_r(username, &pwd, buf, buflen, &pwdp) == 0 && if (username &&
getpwnam_r(username, &pwd, buf, buflen, &pwdp) == 0 &&
pwdp != NULL && pwdp->pw_shell != NULL) { pwdp != NULL && pwdp->pw_shell != NULL) {
if (strlcpy(shell, pwdp->pw_shell, shellsz) < shellsz) if (strlcpy(shell, pwdp->pw_shell, shellsz) < shellsz)
return shell; return shell;
@@ -135,12 +138,12 @@ roken_get_homedir(char *home, size_t homesz)
} }
/* Fallthru to return NULL */ /* Fallthru to return NULL */
#else #else
char user[128]; #ifdef HAVE_GETPWNAM_R
const char *username = roken_get_username(user, sizeof(user));
size_t buflen = 2048; size_t buflen = 2048;
if (sysconf(_SC_GETPW_R_SIZE_MAX) > 0) if (sysconf(_SC_GETPW_R_SIZE_MAX) > 0)
buflen = sysconf(_SC_GETPW_R_SIZE_MAX); buflen = sysconf(_SC_GETPW_R_SIZE_MAX);
#endif
if (issuid()) { if (issuid()) {
errno = 0; errno = 0;
@@ -156,12 +159,15 @@ roken_get_homedir(char *home, size_t homesz)
} }
#ifdef HAVE_GETPWNAM_R #ifdef HAVE_GETPWNAM_R
if (username) { {
char user[128];
const char *username = roken_get_username(user, sizeof(user));
struct passwd pwd; struct passwd pwd;
struct passwd *pwdp; struct passwd *pwdp;
char buf[buflen]; char buf[buflen];
if (getpwnam_r(username, &pwd, buf, buflen, &pwdp) == 0 && if (username &&
getpwnam_r(username, &pwd, buf, buflen, &pwdp) == 0 &&
pwdp != NULL && pwdp->pw_dir != NULL) { pwdp != NULL && pwdp->pw_dir != NULL) {
if (strlcpy(home, pwdp->pw_dir, homesz) < homesz) if (strlcpy(home, pwdp->pw_dir, homesz) < homesz)
return home; return home;
@@ -255,8 +261,13 @@ roken_get_username(char *user, size_t usersz)
} }
} }
#else #else
#ifdef HAVE_GETPWUID_R
size_t buflen = 2048; size_t buflen = 2048;
if (sysconf(_SC_GETPW_R_SIZE_MAX) > 0)
buflen = sysconf(_SC_GETPW_R_SIZE_MAX);
#endif
p = secure_getenv("USER"); p = secure_getenv("USER");
if (p == NULL || p[0] == '\0') if (p == NULL || p[0] == '\0')
p = secure_getenv("LOGNAME"); p = secure_getenv("LOGNAME");
@@ -268,9 +279,6 @@ roken_get_username(char *user, size_t usersz)
} }
#ifdef HAVE_GETPWUID_R #ifdef HAVE_GETPWUID_R
if (sysconf(_SC_GETPW_R_SIZE_MAX) > 0)
buflen = sysconf(_SC_GETPW_R_SIZE_MAX);
{ {
struct passwd pwd; struct passwd pwd;
struct passwd *pwdp; struct passwd *pwdp;