PHP Exec iptables

26
March 14, 2019, at 8:40 PM

I would like exec iptables from PHP.

I've added this line on my sudoers file:

root ALL=(ALL) ALL apache ALL=(root) NOPASSWD:/sbin/iptables -A INPUT -s * -j ACCEPT

I tried also with

root ALL=(root) ALL apache ALL=(root) NOPASSWD:/sbin/iptables -A INPUT -s * -j ACCEPT

My server run on centos 7.

Then when I execute my script I got:

Sorry, user apache is not allowed to execute '/sbin/iptables -A INPUT -s xxx.xxx.xxx.xxx -j ACCEPT' as apache on "servername" why ?

Part of my code:

$ip = $query['ip'];
$pattern = '/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/';
   if(preg_match($pattern , $ip) == true) {      
      $exec_cmd = "/usr/bin/sudo -u apache /sbin/iptables -A INPUT -s $ip -j ACCEPT 2>&1";
      echo('exec: ' . $exec_cmd); 
      $output = [];    
      $retval = 0;
      exec($exec_cmd, $output, $retval);
      print_r($output);
      echo($retval);         
} else {
      echo('Bad IP');
}

Thanks for your help :)

READ ALSO
Can`t parse site on php [duplicate]

Can`t parse site on php [duplicate]

This question already has an answer here:

57
Regular expression in bash or sed

Regular expression in bash or sed

I have a regular expression (PHP) to clean the string from file:

26
Wampserver php root relative path not working anymore

Wampserver php root relative path not working anymore

I am sorry for asking similiar question that have already been asked, but I did not find an answer to my problem

19