How can i verify if a user is root in a PHP script ? What is the command ?
I tried something like that :
exec("su -l login < `echo password`");
but the su command can not receive password…
Note that the machine is isolated from internet so I can run PHP as root if needed.
EDIT:
I don’t want to know if the current user who run the script is root or not.
I have a list of users in my PHP script and I want to know for each login if he has root privileges.
First, ask yourself what exactly defines a login “having root privileges”. AFAICT there are 2 basic solutions.
The old-school way, where sysadmins create multiple accounts with uid 0, which I – and I’m certainly not alone in this – consider to be a nightmare. In this scenario you could check all users in your list using posix_getpwnam and see if their uid matches 0.
The following code snippet does just that, $privileged will contain the users with root privileges :
The other (and imho only sane) way to do this is to add all users with root/administrative privileges to a specific group (wheel or admin are already used in different Linux distributions, find out which one works for you). This scenario is even simpler, since you can use posix_getgrnam to fetch all members in a specific group.
The following code snippet will match an array of logins you provide, and see who’s a member with specific privileges, again $privileged will contain the result (ie. the users in your list that are a member of the group you specified) :