I tried to run su command with PHP shell_exec function. At first I tried:
<?php
echo(shell_exec('echo \'password\' | su user -c \'whoami\' 2>&1;'));
?>
and I got:
su: must be run from a terminal
Then I've read about spawning terminal with python's pty and put it into the shell code:
<?php
echo(shell_exec('echo "if [ -t 1 ] ; then echo "terminal found"; fi; echo \'password\' | su user -c \'whoami\'; exit" | python -c \'import pty; pty.spawn("/bin/bash")\' 2>&1;'));
?>
and here's the output:
if [ -t 1 ] ; then echo terminal found; fi; echo 'password' | su user -c 'whoami'; exit
www-data@server:/some/directory$ if [ -t 1 ] ; then echo terminal found; fi; echo 'password' | su user -c 'whoami'; exit
terminal found
su: must be run from a terminal
exit
Any idea why pty isn't working or what other tool should I use to spawn a terminal? PHP runs behind Apache on Debian.
EDIT: I am searching for a workaround that doesn't require physical access to server or connecting via SSH. Suppose that I want to use su to start sshd and only working servers are FTP and Apache.