[fix] Missing chmod action

This commit is contained in:
alban 2014-09-09 14:52:56 +02:00
parent 58d16d47f9
commit 7c597daa73
4 changed files with 118 additions and 46 deletions

View File

@ -69,7 +69,19 @@ class m_action {
} }
/** /**
* Plans the cration of a dir * Plans the a chmod on file or dir
*
* @param string $filename
* @param int $perms
* @param string $user
* @return boolean
*/
function chmod($filename, $perms, $user = "root") {
return $this->set('chmod', $user, array('filename' => $filename,"perms"=>$perms));
}
/**
* Plans the creation of a dir
* *
* @param string $dir * @param string $dir
* @param int $user * @param int $user
@ -186,6 +198,9 @@ class m_action {
$serialized = serialize($parameters); $serialized = serialize($parameters);
switch ($type) { switch ($type) {
case 'chmod':
$query = "insert into actions values ('','CHMOD','$serialized',now(),'','','$user','');";
break;
case 'create_file': case 'create_file':
$query = "insert into actions values ('','CREATE_FILE','$serialized',now(),'','','$user','');"; $query = "insert into actions values ('','CREATE_FILE','$serialized',now(),'','','$user','');";
break; break;

View File

@ -546,6 +546,7 @@ class m_bro {
* Change les droits d'acces aux fichier de $d du dossier $R en $p * Change les droits d'acces aux fichier de $d du dossier $R en $p
* *
* @global m_err $err * @global m_err $err
* @global m_action $action
* @param string $R Dossier dans lequel se trouve les fichiers renommer. * @param string $R Dossier dans lequel se trouve les fichiers renommer.
* @param boolean $verbose Shall we 'echo' what we did ? * @param boolean $verbose Shall we 'echo' what we did ?
* @return boolean TRUE Si les fichiers ont t renomms, FALSE si une erreur s'est produite. * @return boolean TRUE Si les fichiers ont t renomms, FALSE si une erreur s'est produite.
@ -569,7 +570,7 @@ class m_bro {
} else { } else {
$m=$m ^ 0222; // ugo-w $m=$m ^ 0222; // ugo-w
} }
chmod($absolute."/".$d[$i], $m); $action->chmod($absolute."/".$d[$i], $m);
if ($verbose) { if ($verbose) {
echo "chmod ".sprintf('%o', $m)." file, was ".sprintf('%o', fileperms($absolute."/". $d[$i])). " -- ".$perm[$i]['w']; echo "chmod ".sprintf('%o', $m)." file, was ".sprintf('%o', fileperms($absolute."/". $d[$i])). " -- ".$perm[$i]['w'];
} }

View File

@ -69,6 +69,15 @@ class m_actionTest extends AlterncTest {
$this->assertFileExists($L_INOTIFY_DO_ACTION); $this->assertFileExists($L_INOTIFY_DO_ACTION);
} }
/**
* @covers m_action::chmod
*/
public function testChmod() {
$result = $this->object->chmod(self::TEST_FILE, 0777,"phpunit");
$this->assertEquals(1, $this->getConnection()->getRowCount('actions'));
}
/** /**
* @covers m_action::create_file * @covers m_action::create_file
*/ */

View File

@ -39,34 +39,69 @@
/* /*
Fixme Fixme
- handle error correctly
- check all those cases - check all those cases
*/ */
/////////////////////////////////// ///////////////////////////////////
// Put this var to 1 if you want to enable debug prints // Put this var to 1 if you want to enable debug prints
$debug=1; $debug=0;
$error_raise='';
// Debug function that print infos // Collects errors along execution. If length > 1, an email is sent.
$errorsList=array();
// Bootstraps
require_once("/usr/share/alternc/panel/class/config_nochk.php");
// Script lock through filesystem
$admin->stop_if_jobs_locked();
if( !defined("ALTERNC_DO_ACTION_LOCK")){
define("ALTERNC_DO_ACTION_LOCK",'/var/run/alternc/do_actions_cron.lock');
}
$SCRIPT='/usr/bin/php do_actions.php';
$MY_PID=getmypid();
$FIXPERM='/usr/lib/alternc/fixperms.sh';
/**
*
* Debug function that print infos
*
* @global int $debug
* @param type $mess
*/
function d($mess){ function d($mess){
global $debug; global $debug;
if ($debug == 1) if ($debug == 1)
echo "$mess\n"; echo "$mess\n";
} }
// Function to mail the panel's administrator if something failed /**
* Function to mail the panel's administrator if something failed
* @global array $errorsList
* @global type $L_FQDN
*/
function mail_it(){ function mail_it(){
global $error_raise,$L_FQDN; global $errorsList,$L_FQDN;
mail("alterncpanel@$L_FQDN",'Script do_actions.php issues',"\n Errors reporting mail:\n\n$error_raise"); // Forces array
if( !is_array($errorsList)){
$errorsList = array($errorsList);
}
// Builds message from array
$msg = implode("\n", $errorsList);
// Attempts to send email
// @todo log if fails
mail("alterncpanel@$L_FQDN",'Script do_actions.php issues',"\n Errors reporting mail:\n\n$msg");
} }
/* /**
* $command: the command * Common routine for system calls
* $parameters: of the command (they are going to be protected) *
* return array('output'=>'output of exec', 'return_var'=>'returned integer of exec') * @param type $command the command
* @param type $parameters of the command (they are going to be protected)
* @return array('output'=>'output of exec', 'return_var'=>'returned integer of exec')
*/ */
function execute_cmd($command, $parameters=array()) { function execute_cmd($command, $parameters=array()) {
$cmd_line = "$command "; $cmd_line = "$command ";
@ -84,20 +119,12 @@ function execute_cmd($command, $parameters=array()) {
return array('executed' => $cmd_line, 'output'=>$output, 'return_var'=>$code); return array('executed' => $cmd_line, 'output'=>$output, 'return_var'=>$code);
} }
require_once("/usr/share/alternc/panel/class/config_nochk.php");
$admin->stop_if_jobs_locked();
$LOCK_FILE='/var/run/alternc/do_actions_cron.lock';
$SCRIPT='/usr/bin/php do_actions.php';
$MY_PID=getmypid();
$FIXPERM='/usr/lib/alternc/fixperms.sh';
// Check if script isn't already running // Check if script isn't already running
if (file_exists($LOCK_FILE) !== false){ if (file_exists(ALTERNC_DO_ACTION_LOCK) !== false){
d("Lock file already exists. "); d("Lock file already exists. ");
// Check if file is in process list // Check if file is in process list
$PID=file_get_contents($LOCK_FILE); $PID=file_get_contents(ALTERNC_DO_ACTION_LOCK);
d("My PID is $MY_PID, PID in the lock file is $PID"); d("My PID is $MY_PID, PID in the lock file is $PID");
if ($PID == exec("pidof $SCRIPT | tr ' ' '\n' | grep -v $MY_PID")){ if ($PID == exec("pidof $SCRIPT | tr ' ' '\n' | grep -v $MY_PID")){
// Previous cron is not finished yet, just exit // Previous cron is not finished yet, just exit
@ -105,16 +132,16 @@ if (file_exists($LOCK_FILE) !== false){
exit(0); exit(0);
}else{ }else{
// Previous cron failed! // Previous cron failed!
$error_raise.="Lock file already exists. No process with PID $PID found! Previous cron failed...\n"; $errorsList[]="Lock file already exists. No process with PID $PID found! Previous cron failed...\n";
// No need to remove anything, we're going to recreate it // No need to remove anything, we're going to recreate it
//d("Removing lock file and trying to process the failed action..."); //d("Removing lock file and trying to process the failed action...");
// Delete the lock and continue to the next action // Delete the lock and continue to the next action
//unlink($LOCK_FILE); //unlink(ALTERNC_DO_ACTION_LOCK);
// Lock with the current script's PID // Lock with the current script's PID
if (file_put_contents($LOCK_FILE,$MY_PID) === false){ if (file_put_contents(ALTERNC_DO_ACTION_LOCK,$MY_PID) === false){
$error_raise.="Cannot open/write $LOCK_FILE\n"; $errorsList[]="Cannot open/write ALTERNC_DO_ACTION_LOCK\n";
mail_it(); mail_it();
exit(1); exit(1);
} }
@ -131,9 +158,9 @@ if (file_exists($LOCK_FILE) !== false){
$action->reset_job($c["id"]); $action->reset_job($c["id"]);
}else{ }else{
// We can't resume the others types, notify the fail and finish this action // We can't resume the others types, notify the fail and finish this action
$error_raise.="Can't resume the job n°".$c["id"]." action '".$c["type"]."', finishing it with a fail status.\n"; $errorsList[]="Can't resume the job n°".$c["id"]." action '".$c["type"]."', finishing it with a fail status.\n";
if(!$action->finish($c["id"],"Fail: Previous script crashed while processing this action, cannot resume it.")){ if(!$action->finish($c["id"],"Fail: Previous script crashed while processing this action, cannot resume it.")){
$error_raise.="Cannot finish the action! Error while inserting the error value in the DB for action n°".$c["id"]." : action '".$c["type"]."'\n"; $errorsList[]="Cannot finish the action! Error while inserting the error value in the DB for action n°".$c["id"]." : action '".$c["type"]."'\n";
break; // Else we go into an infinite loop... AAAAHHHHHH break; // Else we go into an infinite loop... AAAAHHHHHH
} }
} }
@ -141,8 +168,8 @@ if (file_exists($LOCK_FILE) !== false){
} }
}else{ }else{
// Lock with the current script's PID // Lock with the current script's PID
if (file_put_contents($LOCK_FILE,$MY_PID) === false){ if (file_put_contents(ALTERNC_DO_ACTION_LOCK,$MY_PID) === false){
$error_raise.="Cannot open/write $LOCK_FILE\n"; $errorsList[]="Cannot open/write ALTERNC_DO_ACTION_LOCK\n";
mail_it(); mail_it();
exit(1); exit(1);
} }
@ -157,7 +184,6 @@ while ($rr=$action->get_action()){
$SU="su ".$r["user"]." 2>&1 ;"; $SU="su ".$r["user"]." 2>&1 ;";
else else
$SU=""; $SU="";
unset($output);
// We lock the action // We lock the action
d("-----------\nBeginning action n°".$r["id"]); d("-----------\nBeginning action n°".$r["id"]);
$action->begin($r["id"]); $action->begin($r["id"]);
@ -170,19 +196,36 @@ while ($rr=$action->get_action()){
// Create the directory and make parent directories as needed // Create the directory and make parent directories as needed
#@exec("$FIXPERM -u ".$params["uid"]." 2>&1", $trash, $code); #@exec("$FIXPERM -u ".$params["uid"]." 2>&1", $trash, $code);
$returned = execute_cmd("$FIXPERM -u", $params["uid"]); $returned = execute_cmd("$FIXPERM -u", $params["uid"]);
break;
case "CHMOD" :
$filename=$params["file"];
$perms=$params["perms"];
// Checks the file or directory exists
if( !is_dir($filename) && ! is_file($filename)){
$errorsList=array("Fail: cannot create ".$params["dst"]);
}
// Checks the perms are correct
else if ( !is_int( $perms)){
$errorsList=array("Fail: cannot create ".$params["dst"]);
}
// Attempts to change the rights on the file or directory
else if( !chmod($filename, $perms)) {
$errorsList=array("Fail: cannot create ".$params["dst"]);
}
break; break;
case "CREATE_FILE" : case "CREATE_FILE" :
if(!file_exists($params["file"])) { if(!file_exists($params["file"])) {
#@exec("$SU touch ".$params["file"]." 2>&1 ; echo '".$params["content"]."' > '".$params["file"]."' 2>&1", $output); #@exec("$SU touch ".$params["file"]." 2>&1 ; echo '".$params["content"]."' > '".$params["file"]."' 2>&1", $output);
if ( file_put_contents($params["file"], $params["content"]) === false ) { if ( file_put_contents($params["file"], $params["content"]) === false ) {
$log_error=array("Fail: can't write into file ".$params["file"]); $errorsList=array("Fail: can't write into file ".$params["file"]);
} else { } else {
if (!chown($params["file"], $r["user"])) { if (!chown($params["file"], $r["user"])) {
$log_error=array("Fail: cannot chown ".$params["file"]); $errorsList=array("Fail: cannot chown ".$params["file"]);
} }
} }
} else { } else {
$log_error=array("Fail: file already exists ".$params["file"]); $errorsList=array("Fail: file already exists ".$params["file"]);
} }
break; break;
case "CREATE_DIR" : case "CREATE_DIR" :
@ -203,18 +246,22 @@ while ($rr=$action->get_action()){
$returned = execute_cmd("$SU mv -f", array($params["src"], $params["dst"])); $returned = execute_cmd("$SU mv -f", array($params["src"], $params["dst"]));
} }
} else { //is_dir false } else { //is_dir false
$log_error=array("Fail: cannot create ".$params["dst"]); $errorsList=array("Fail: cannot create ".$params["dst"]);
} // is_dir } // is_dir
break; break;
case "FIX_DIR" : case "FIX_DIR" :
$returned = execute_cmd($FIXPERM, array('-d', $params["dir"])); $returned = execute_cmd($FIXPERM, array('-d', $params["dir"]));
if($returned['return_val'] != 0) $log_error=array("Fixperms.sh failed, returned error code : ".$returned['return_val']); if($returned['return_val'] != 0) {
$errorsList=array("Fixperms.sh failed, returned error code : ".$returned['return_val']);
}
break; break;
case "FIX_FILE" : case "FIX_FILE" :
#@exec("$FIXPERM -f ".$params["file"]." 2>&1", $trash, $code); #@exec("$FIXPERM -f ".$params["file"]." 2>&1", $trash, $code);
$returned = execute_cmd($FIXPERM, array('-f', $params["file"])); $returned = execute_cmd($FIXPERM, array('-f', $params["file"]));
if($returned['return_val'] != 0) $log_error=array("Fixperms.sh failed, returned error code : ".$returned['return_val']); if($returned['return_val'] != 0){
$errorsList=array("Fixperms.sh failed, returned error code : ".$returned['return_val']);
}
break; break;
default : default :
$output=array("Fail: Sorry dude, i do not know this type of action"); $output=array("Fail: Sorry dude, i do not know this type of action");
@ -223,24 +270,24 @@ while ($rr=$action->get_action()){
// Get the error (if exists). // Get the error (if exists).
if(isset($output[0])){ if(isset($output[0])){
$return=$output[0]; $return=$output[0];
$error_raise.="\nAction n°".$r["id"]." '".$r["type"]."' failed! With user: ".$r["user"]."\nHere is the complete output:\n".print_r($output); $errorsList[]="\nAction n°".$r["id"]." '".$r["type"]."' failed! With user: ".$r["user"]."\nHere is the complete output:\n".print_r($output);
} }
// We finished the action, notify the DB. // We finished the action, notify the DB.
d("Finishing... return value is : $return\n"); d("Finishing... return value is : $return\n");
if(!$action->finish($r["id"],addslashes($return))){ if(!$action->finish($r["id"],addslashes($return))){
$error_raise.="Cannot finish the action! Error while inserting the error value in the DB for action n°".$r["id"]." : action '".$r["type"]."'\nReturn value: ".addslashes($return)."\n"; $errorsList[]="Cannot finish the action! Error while inserting the error value in the DB for action n°".$r["id"]." : action '".$r["type"]."'\nReturn value: ".addslashes($return)."\n";
break; // Else we go into an infinite loop... AAAAHHHHHH break; // Else we go into an infinite loop... AAAAHHHHHH
} }
} }
// If something have failed, notify it to the admin // If an error occured, notify it to the admin
if($error_raise !== '') { if(count($errorsList)) {
mail_it(); mail_it();
} }
// Unlock the script // Unlock the script
unlink($LOCK_FILE); // @todo This could be handled by m_admin
unlink(ALTERNC_DO_ACTION_LOCK);
// Exit this script // Exit this script
exit(0); exit(0);
?>