Forum Moderators: coopster
--------------- /path/to/file/filename.txt
----i---------- /path/to/file/filename.txt
I get the output from the bash script, but the file's "immutable" setting doesn't change.
While that processing is taking place, I can't allow the user to be messing with the files.
echo shell_exec('sudo chattr +i /tmp/test.txt 2>&1'); (redirecting strerr to stdout) I got the error sudo: no tty present and no askpass program specified, to get around this I edited the sudoers file to allow chattr to be run by the apache user without a password, like this:
www-data ALL=NOPASSWD: /usr/bin/chattr
I need to think about the consequences of many user processing files with the same name
www-data (the apache user on Ubuntu) can run from an host (ALL) using no password (NOPASSWD) the command /usr/bin/chattr as root. # php chattr.phpthen it worked great. But if I hit that same script from a browser, it would do nothing.
# yum install libgearman libgearman-devel gearmand
# service gearmand start
# chkconfig gearmand on
# pear install Net_Gearman
<?php
class Net_Gearman_Job_Chattr extends Net_Gearman_Job_Common{
public function run($arg){
$filepath = $arg[0]['fullpath'];
shell_exec("chattr +i ".escapeshellarg($filepath));
}
}
?>
try{
gm::log_msg("[gm_worker] starting worker...");
$worker = new Net_Gearman_Worker(gm::$servers);
$worker->addAbility('Chattr');
$worker->beginWork();
}
# php gm_worker.php
# php gm_worker.php &
<?php
$fullpath = $_GET['filepath'];
require_once ('/path/to/gm_shared.php');
require_once ('Net/Gearman/Client.php');
$gmclient = new Net_Gearman_Client(gm::$servers);
$gmclient->Chattr(array(
array(
"fullpath"=>$fullpath
)
));
?>
I knew from all my previous experiments that in no way would PHP/Apache ever let me run a chattr command.
sudo command, how to call a command from PHP and security implications. sudo command, unlike the similar su command is commonly configured to request the password of the user running it, rather than the password of the target user. The benefit of this is that a user can be granted the privileges of a user (for example root) without giving out that users password. Another powerful feature is the fine grain control it's config file gives over which users can run what commands and as which user. chattr command mentioned above the command modifies extended file system attributes of a file and cannot be run by a non-root user even on files they own. <?php
echo shell_exec('chattr +i /tmp/testfile.txt 2>&1');
?>
chattr: Permission denied while setting flags on /tmp/testfile.txt 2>&1 at the end of the command redirects stderr into stdout allowing you to see the error message. sudo program, it has a quite complex syntax and is a little difficult to get to grips with at first but it is powerful and allows for fine grain control over what commands can be run and by which users. /etc/sudoers chattr program we add the following line to the end of the sudoers file: apache ALL=NOPASSWD: /usr/bin/chattr
chattr. <?php
echo shell_exec('whoami');
?>
chattr is at a different location, the following shell command will find it: whereis chattr
<?php
echo shell_exec('sudo chattr +i /tmp/testfile.txt 2>&1');
?>
chattr command with root privileges. define('UPLOAD_DIR', realpath('/tmp'));
function immutable($file){
$path = realpath(UPLOAD_DIR . '/' . $file);
//is the file within the upload directory and is it readable.
if($path !== false && strpos($path, UPLOAD_DIR . '/') === 0 && is_readable($path)){
$r = shell_exec('sudo chattr +i ' . $path . ' 2>&1');
//chattr returns nothing on success.
if(empty($r)) return true;
}
return false;
}
realpath but could be included if you're feeling paranoid, also is_writable [php.net] could be used instead of is_readable so the command will only be run against file the script can write.