Change the way username is obtained

Username should be obtained by getting user name for effective UID. Using USERNAME or USER variables is not the right way (when you just "su" and not "su - " it keeps previous username):
id  :  uid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel) context=user_u:system_r:unconfined_t
./validate.php:   [FAIL]    You need to run this script as root
echo $USER: <name of user that did su>
This commit is contained in:
deutor
2016-08-31 10:20:07 +02:00
committed by GitHub
parent 70ef3dadf0
commit 74675a7b00

View File

@@ -48,7 +48,12 @@ if (strstr(`php -ln config.php`, 'No syntax errors detected')) {
}
// Check we are running this as the root user
$username = getenv('USERNAME') ?: getenv('USER');//http://php.net/manual/en/function.get-current-user.php
if (function_exists('posix_getpwuid')) {
$userinfo = posix_getpwuid( posix_geteuid() );
$username = $userinfo['name'];
} else {
$username = getenv('USERNAME') ?: getenv('USER');//http://php.net/manual/en/function.get-current-user.php
}
if ($username !== 'root') {
print_fail("You need to run this script as root");
}