jcvj
jcvj

Reputation: 130

PHP shell_exec('whoami; ls /var/lib/nagios3 2>&1'); Permissions denied whilst fine in shell

I have a little PHP script, running in a Vagrant VM (shouldn't matter, but who knows):

<?php
  echo 'euid' + posix_geteuid(); // Prints 33, which is the ID of www-data
  echo shell_exec('whoami; ls /var/lib/nagios3 2>&1'); // Prints www-data ls: cannot open directory /var/lib/nagios3
  echo shell_exec( 'whoami' ); // Prints www-data
?>

/var/lib/nagios3 looks like this:

drwxr-x---  4 nagios  nagios   4096 Jan  6 18:00 nagios3/

www-data is a member of the group nagios, I set that up.

And if I run this in the shell:

ssh [email protected]....
$ cd /var/lib/nagios3
$ ls
retention.dat  rw  spool
$ ls /var/lib/nagios3
retention.dat  rw  spool

Everything is just fine!

Can anybody please explain me this odd behaviour? I'd be very thankful!

Upvotes: 2

Views: 297

Answers (2)

grebneke
grebneke

Reputation: 4494

You should also check what id says, does it list nagios in the groups for www-data?

echo shell_exec('id'); // uid=33(web-data) gid=... groups=... IS nagios IN HERE?

If you added www-data to group nagios after starting the webserver, you might need to restart the webserver, or even reboot. At least this happens with normal user sessions in Linux - you add yourself to some group (audio/video etc) and need to logout/login before the change takes place. When you ssh into the box you are given a fresh shell with the new permissions, but the webserver is still running under the old permissions.

Upvotes: 2

dkakoti
dkakoti

Reputation: 667

You have to give 775 permission to that php file

chmod 775 

Upvotes: 0

Related Questions