Skip to content

Commit 385ede4

Browse files
committed
Split off UID to username translation of php_get_current_user()
1 parent 5d2f0cb commit 385ede4

File tree

1 file changed

+56
-42
lines changed

1 file changed

+56
-42
lines changed

main/main.c

Lines changed: 56 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1492,6 +1492,56 @@ static ZEND_COLD void php_error_cb(int orig_type, zend_string *error_filename, c
14921492
}
14931493
/* }}} */
14941494

1495+
#ifndef PHP_WIN32
1496+
static char *php_translate_uid_to_username(uid_t uid, size_t *len)
1497+
{
1498+
struct passwd *pwd;
1499+
#if defined(ZTS) && defined(HAVE_GETPWUID_R) && defined(_SC_GETPW_R_SIZE_MAX)
1500+
struct passwd _pw;
1501+
struct passwd *retpwptr = NULL;
1502+
int pwbuflen = sysconf(_SC_GETPW_R_SIZE_MAX);
1503+
char *pwbuf;
1504+
int err;
1505+
1506+
if (pwbuflen < 1) {
1507+
pwbuflen = 1024;
1508+
}
1509+
# if ZEND_DEBUG
1510+
/* Test retry logic */
1511+
pwbuflen = 1;
1512+
# endif
1513+
pwbuf = emalloc(pwbuflen);
1514+
1515+
try_again:
1516+
err = getpwuid_r(uid, &_pw, pwbuf, pwbuflen, &retpwptr);
1517+
if (err != 0) {
1518+
if (err == ERANGE) {
1519+
pwbuflen *= 2;
1520+
pwbuf = erealloc(pwbuf, pwbuflen);
1521+
goto try_again;
1522+
}
1523+
efree(pwbuf);
1524+
return NULL;
1525+
}
1526+
if (retpwptr == NULL) {
1527+
efree(pwbuf);
1528+
return NULL;
1529+
}
1530+
pwd = &_pw;
1531+
#else
1532+
if ((pwd=getpwuid(uid))==NULL) {
1533+
return NULL;
1534+
}
1535+
#endif
1536+
*len = strlen(pwd->pw_name);
1537+
char *result = estrndup(pwd->pw_name, *len);
1538+
#if defined(ZTS) && defined(HAVE_GETPWUID_R) && defined(_SC_GETPW_R_SIZE_MAX)
1539+
efree(pwbuf);
1540+
#endif
1541+
return result;
1542+
}
1543+
#endif
1544+
14951545
/* {{{ php_get_current_user */
14961546
PHPAPI char *php_get_current_user(void)
14971547
{
@@ -1524,50 +1574,14 @@ PHPAPI char *php_get_current_user(void)
15241574
free(name);
15251575
return SG(request_info).current_user;
15261576
#else
1527-
struct passwd *pwd;
1528-
#if defined(ZTS) && defined(HAVE_GETPWUID_R) && defined(_SC_GETPW_R_SIZE_MAX)
1529-
struct passwd _pw;
1530-
struct passwd *retpwptr = NULL;
1531-
int pwbuflen = sysconf(_SC_GETPW_R_SIZE_MAX);
1532-
char *pwbuf;
1533-
int err;
1534-
1535-
if (pwbuflen < 1) {
1536-
pwbuflen = 1024;
1537-
}
1538-
# if ZEND_DEBUG
1539-
/* Test retry logic */
1540-
pwbuflen = 1;
1541-
# endif
1542-
pwbuf = emalloc(pwbuflen);
1543-
1544-
try_again:
1545-
err = getpwuid_r(pstat->st_uid, &_pw, pwbuf, pwbuflen, &retpwptr);
1546-
if (err != 0) {
1547-
if (err == ERANGE) {
1548-
pwbuflen *= 2;
1549-
pwbuf = erealloc(pwbuf, pwbuflen);
1550-
goto try_again;
1551-
}
1552-
efree(pwbuf);
1577+
size_t len;
1578+
char *username = php_translate_uid_to_username(pstat->st_uid, &len);
1579+
if (!username) {
15531580
return "";
15541581
}
1555-
if (retpwptr == NULL) {
1556-
efree(pwbuf);
1557-
return "";
1558-
}
1559-
pwd = &_pw;
1560-
#else
1561-
if ((pwd=getpwuid(pstat->st_uid))==NULL) {
1562-
return "";
1563-
}
1564-
#endif
1565-
SG(request_info).current_user_length = strlen(pwd->pw_name);
1566-
SG(request_info).current_user = estrndup(pwd->pw_name, SG(request_info).current_user_length);
1567-
#if defined(ZTS) && defined(HAVE_GETPWUID_R) && defined(_SC_GETPW_R_SIZE_MAX)
1568-
efree(pwbuf);
1569-
#endif
1570-
return SG(request_info).current_user;
1582+
SG(request_info).current_user_length = (int) len;
1583+
SG(request_info).current_user = username;
1584+
return username;
15711585
#endif
15721586
}
15731587
}

0 commit comments

Comments
 (0)