0

I have a bash script that takes a parameter is called in PHP by shell_exec(script.sh parameter). Basically, my goal is to call a script that is owned by another user that is not apache.

The script.sh script is a file that contains the following (right now there are some error handling commands):

#/bin/bash
whoami>>whoami
echo $1 >> parameter
while read f; do
env>>envoutput
sudo -i -u archivescriptowner /path/to/archivescript.sh -command archive >> output
done < $1

In my /etc/sudoers file , I have the following:

apache ALL=(archivescriptowner) NOPASSWD: /bin/bash -c /path/to/archivescript.sh  * 

When I run this script as by running su -s /bin/bash apache and pass a parameter, it works.

When I run it via my button in php, archivescript.sh does not execute

The whoami file has apache written to it

The parameter file has the right file written to it

env shows the following

Term=xterm
LD_LIBRARY_PATH=/path/to/library
PATH=/sbin/:usr/sbin:/bin:/usr/bin
PWD=/var/www/html
LANG=C
SHLVL=4
=/bin/env

PWD is outputting right, that is where my script is right now, it will be moved in the future.

The output file when it is ran by the button click is blank.

I am at a loss as to why this is not working. Any insight would be helpful. Please let me know if I need to give any additional information.

4
  • I think the sudoers entry should just say /path/to/archivescript.sh, without the /bin/bash -c before it. Commented Sep 30, 2015 at 22:26
  • No, that's unlikely to have anything to do with the problem. There's quite lot here which is very dd here - top of my list of things to check is whether the '-i' flag is causing complications and if the webserver is running chroot. Commented Sep 30, 2015 at 22:42
  • @Barmar Taking away /bin/bash -c prompts for a password when logging in as apache user and does not work for the php either. That being said, if I remove sudo -i, taking away /bin/bash -c works for the apache user, but not with php. Commented Sep 30, 2015 at 23:02
  • @symcbean I am not sure what you meant by "There's quite lot here which is very dd here" but if you are saying that there is a lot that it is odd, could you please elaborate so I can run things properly? Webserver is not running chroot. If I remove sudo -i, taking away /bin/bash -c works for the apache user, but not with php. Commented Sep 30, 2015 at 23:02

1 Answer 1

2

I recently published a project that allows PHP to obtain and interact with a real Bash shell. Get it here: https://github.com/merlinthemagic/MTS

After downloading you would simply use the following code:

$shell    = \MTS\Factories::getDevices()->getLocalHost()->getShell('bash', true);

$return1  = $shell->exeCmd('/path/to/archivescript.sh');

echo $return1; //return from your script
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.