4

I am trying to execute a C program using the shell_exec command, which needs arguments to be passed. It is working for one input, but not working for others. I tried to run the C program through terminal, it is working for all the inputs.

This is my execprog.php file. I have to give 2 inputs as command line arguments to file. /var/www/project is the path.

$query = "/var/www/project/./a.out /var/www/project/constraints.txt /var/www/project/constraints_keyword.txt /var/www/project/FIB.txt /var/www/project/ANS.txt";

echo $query;

$var = shell_exec($query);

echo $var;
Dan Lowe
  • 51,713
  • 20
  • 123
  • 112
  • 2
    Welcome to Stack Overflow! You can take the [tour] first and learn [ask] a good question and create a [mcve]. That makes it easier for us to help you. – Katie Feb 04 '17 at 14:34
  • try quoting the arguments: http://stackoverflow.com/a/16932201/454827 – ZiTAL Feb 06 '17 at 08:38

2 Answers2

0
<?php
$query = "/var/www/project/./a.out";
$arguments = array
(
    '/var/www/project/constraints.txt',
    '/var/www/project/constraints_keyword.txt',
    '/var/www/project/FIB.txt',
    '/var/www/project/ANS.txt'
);
$string = '';
for($i=0;$i<count($arguments);$i++)
    $string.= ' %s';

$command = vsprintf("{$query}{$string}", $arguments);
$var = shell_exec($command);
echo $var;
ZiTAL
  • 3,466
  • 8
  • 35
  • 50
  • Not working..!! I am printing the query i.e $command on the browser..if i run this query which is printed on the browser on the terminal its working fine.. – Keyur Kulkarni Feb 06 '17 at 14:32
  • you can change web server user and group to your own account, restart server and check it, if you use apache in debian/ubuntu is here: /etc/apache2/envvars – ZiTAL Feb 06 '17 at 15:31
  • login as root and run this script: `/bin/su -c /var/www/project/a.out /var/www/project/constraints.txt /var/www/project/constraints_keyword.txt /var/www/project/FIB.txt /var/www/project/ANS.txt www-data` http://serverfault.com/a/362198/143613 – ZiTAL Feb 06 '17 at 15:37
  • It is sayin Unknown id: /var/www/project/input1.txt. – Keyur Kulkarni Feb 06 '17 at 16:59
  • the path which you told /etc/apache2/envvars. This envvars file is read-only file. – Keyur Kulkarni Feb 06 '17 at 17:19
0

As you it works on the terminal and not on apache then apache's php.ini file may be disabling the use of shell_exec().

See http://www.php.net/manual/en/ini.core.php#ini.disable-functions

Your apache's php.ini file may look something like

disable_functions=exec,passthru,shell_exec,system,proc_open,popen

Remove shell_exec from this list and restart the web server, although this is a security risk and I don't recommend it.

In general functions such as exec,shell_exec and system are always used to execute the external programs. Even a shell command can also be executed. If these two functions are enabled then a user can enter any command as input and execute into your server. So usually people disable in apache config as disable_functions to secure their site.

It works for me - Here is test run

Sample test c code

[akshay@gold tmp]$ cat test.c
#include<stdio.h>

int main(int args, char *argv[]) {
 int i = 0;
 for (i = 0; i < args; i++)
 printf("Arg[%d] = %s\n",i, argv[i]);
 return 0;
}

Compile

[akshay@gold tmp]$ gcc test.c 

Sample php script

[akshay@gold tmp]$ cat test.php
<?php
$query = "/tmp/./a.out /var/www/project/constraints.txt /var/www/project/constraints_keyword.txt /var/www/project/FIB.txt /var/www/project/ANS.txt";
$var = shell_exec($query);
echo $var;
?>

Execution and output

[akshay@gold tmp]$ php test.php
Arg[0] = /tmp/./a.out
Arg[1] = /var/www/project/constraints.txt
Arg[2] = /var/www/project/constraints_keyword.txt
Arg[3] = /var/www/project/FIB.txt
Arg[4] = /var/www/project/ANS.txt
Akshay Hegde
  • 16,536
  • 2
  • 22
  • 36