I have been following this answer and I can't figure out what the hell I'm doing wrong. When I call my api endpoint (via postman) I get following error, when my script tries to generate and compile script c script.
My php code
exec('
cat > wrapper.c <<CONTENT
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int
main (int argc, char *argv[])
{
setuid (0);
system ("/bin/sh /var/www/html/myapp/script.sh");
return 0;
}
CONTENT
gcc wrapper.c -o php_root 2>&1
', $output, $returnVar);
var_dump($output);
var_dump($returnVar);
die();
And the error that I get is gcc: error trying to exec 'cc1': execvp: No such file or directory'
which happens when gcc wrapper.c -o php_root 2>&1
command is executed
Running all those commands in bash works fine, but when they are executed via PHP exec, I get the error.
gcc -v
(venv) certify-dev: /var/www/html/certify/public $ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/5/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 5.4.0-6ubuntu1~16.04.10' --with-bugurl=file:///usr/share/doc/gcc-5/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-5 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-5-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-5-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-5-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.10)
Can you guy please help me find out where the problem is. If you have any additional questions, please let me know and I will provide answer. Thank you!
UPDATE
@squeamish notified me, I should provide full gcc path and this was a case, but now I have another error
Updated line
/usr/bin/gcc wrapper.c -o php_root 2>&1
Error
0 => string 'collect2: fatal error: cannot find 'ld'' (length=39)
1 => string 'compilation terminated.' (length=23)
2. UPDATE
After checking my PATHs (suggested by @ChrisTurner) for php I found out that my shell is missing php executables...
my php code
$phpFinder = new PhpExecutableFinder;
if (!$phpPath = $phpFinder->find()) {
throw new \Exception('The php executable could not be found, add it to your PATH environment variable and try again');
}
var_dump($phpPath); /usr/bin/php
var_dump(PHP_BINDIR); /usr/bin/php
and shell
dev: /var/www/html/myapp $ sudo -H -u www-data bash -c 'echo $PATH'
outputs
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/local/go/bin
That's why I added export PATH="$PATH:/usr/bin/php"
in to my ~/.bashrc and this are my PATHs now in bash
myApp: /var/www/html/certify $ sudo -H -u www-data bash -c 'echo $PATH'
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/local/go/bin:/usr/bin/php
Code is still throwing errors 'collect2: fatal error: cannot find 'ld''