简体   繁体   中英

PHP Exec Sudo/Exim commands

I'm trying to get a sudo command to execute from within PHP. For simplicity sake, I'm using passthru wrapped in pre tags. Long story short, I want to get some exim stats.

I'm on a CentOS box.

After logging in via shell, I added the following line via visudo.

myuser      ALL = NOPASSWD: /usr/sbin/exiwhat

I can successfully execute the exiwhat command using either of the following commands when shelled in.

sudo -u myuser -H /usr/sbin/exiwhat
sudo -u myuser -H exiwhat

I've even tried becoming the the user via su and am still successful.

su myuser
sudo /usr/sbin/exiwhat
-or-
sudo exiwhat

My php script looks a little something like this.

<?php
echo '<pre>';
passthru('whoami');
echo "\n1)";
passthru('sudo exiwhat');
echo "\n2)";
passthru('sudo /usr/sbin/exiwhat');
echo "\n3)";
passthru('exiwhat');
echo "\n4)";
passthru('/usr/sbin/exiwhat');
echo "\n";
echo '</pre>';

All I get back is:

myuser

1)
2)
3)
4)No exim process data

I've tried the same with

exec('exiwhat',$output);

but output returns as an empty array.

I can get simple things like "ps aux" or "dir" to work just fine. Please help.

Possible Duplicate: How to call shell script from php that requires SUDO?

Note: I can run exiwhat via command line and get a good chunk of output. We have several hundred emails in the queue at any point in time.

UPDATE: Per another thread, I added " 2>&1" to each command and got the following

myuser

1)sudo: sorry, you must have a tty to run sudo

2)sudo: sorry, you must have a tty to run sudo

3)sh: exiwhat: command not found

4)/bin/rm: cannot remove `/var/spool/exim/exim-process.info': Permission denied
exim(770): Operation not permitted
exim(8016): Operation not permitted
exim(15618): Operation not permitted
exim(15626): Operation not permitted
exim(16751): Operation not permitted
exim(16765): Operation not permitted
exim(32207): Operation not permitted
exim: no process killed
No exim process data

UPDATE: I added the following line via visudo and my sudo commands now work

Defaults:myuser !requiretty

Is that save to leave like that though?

Edit your /etc/sudoers file and remove the line about Default requiretty.

**Update I guess this options enabled by default so add something like the following instead:

Defaults:myuser        !requiretty

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM