Okay, here's my solution, hopefully someone can sanity-check it.
I should say my server uses suPHP so my sites run with permissions of their owners.
The best approach seems to be to allow your web user to sudo the command.
Step 1 : I create a file /etc/sudoers.d/webuser with one line :
webuser ALL=NOPASSWD: /usr/sbin/csf
Please note (a) this requires /etc/sudoers to have the line :
#includedir /etc/sudoers.d
and (b) recommend using [font=courier]visudo[/font] to make sure you don't break your sudoers file.
This allows my webuser to do stuff like
Code: Select all
shell_exec("sudo /usr/sbin/csf -d");
Step 2 create a function to add an IP and comment :
Code: Select all
function AddIPToCSF($ip,$comment) {
$cmd = "sudo /usr/sbin/csf -d";
$s = $cmd . " " . $ip . " " . $comment;
$output = shell_exec($s);
return "<p>" . $output . "</p>";
}
Step 3 : Call this function when needed e.g.
Code: Select all
<?php
require_once("../path_to_function");
$ip = "112.215.66.76";
$comment = "Added, random hacker/injection attempt etc";
$res = AddIPToCSF($ip,$comment);
echo $res;
?>
The implication for security is that in theory, any malicious web script can use the CSF command line options which are very powerful and could disable CSF completely - am I right ?