2013-04-19 15:09:34 +00:00
#!/usr/bin/php -q
< ? php
/*
$Id : do_actions . php , v 1.0 2013 / 04 / 19 13 : 40 : 32 axel Exp $
----------------------------------------------------------------------
AlternC - Web Hosting System
Copyright ( C ) 2002 by the AlternC Development Team .
http :// alternc . org /
----------------------------------------------------------------------
Based on :
Valentin Lacambre ' s web hosting softwares : http :// altern . org /
----------------------------------------------------------------------
LICENSE
This program is free software ; you can redistribute it and / or
modify it under the terms of the GNU General Public License ( GPL )
as published by the Free Software Foundation ; either version 2
of the License , or ( at your option ) any later version .
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
To read the license please visit http :// www . gnu . org / copyleft / gpl . html
----------------------------------------------------------------------
Original Author of file : Axel Roger
Purpose of file : Do planed actions on files / directories .
----------------------------------------------------------------------
*/
/**
* This script check the MySQL DB for actions to do , and do them one by one .
*
* @ copyright AlternC - Team 2002 - 2013 http :// alternc . org /
*/
2014-03-07 14:57:55 +00:00
//////////////////////////////////
/*
Fixme
- check all those cases
*/
///////////////////////////////////
2013-04-23 13:32:37 +00:00
// Put this var to 1 if you want to enable debug prints
2014-09-09 12:52:56 +00:00
$debug = 0 ;
// Collects errors along execution. If length > 1, an email is sent.
$errorsList = array ();
2013-04-23 13:32:37 +00:00
2014-09-09 12:52:56 +00:00
// 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
*/
2013-04-23 13:32:37 +00:00
function d ( $mess ){
global $debug ;
if ( $debug == 1 )
echo " $mess\n " ;
}
2014-09-09 12:52:56 +00:00
/**
* Function to mail the panel ' s administrator if something failed
* @ global array $errorsList
* @ global type $L_FQDN
*/
2013-04-25 13:05:27 +00:00
function mail_it (){
2014-09-09 12:52:56 +00:00
global $errorsList , $L_FQDN ;
// 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 " );
2013-04-25 13:05:27 +00:00
}
2014-09-09 12:52:56 +00:00
/**
* Common routine for system calls
*
* @ 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' )
2014-03-07 14:57:55 +00:00
*/
function execute_cmd ( $command , $parameters = array ()) {
$cmd_line = " $command " ;
if ( ! empty ( $parameters )) {
if ( is_array ( $parameters )) {
foreach ( $parameters as $pp ) {
$cmd_line .= " " . escapeshellarg ( $pp ) . " " ;
}
} else {
$cmd_line .= " " . escapeshellarg ( $parameters ) . " " ;
}
}
$cmd_line .= " 2>&1 " ;
exec ( $cmd_line , $output , $code );
return array ( 'executed' => $cmd_line , 'output' => $output , 'return_var' => $code );
}
2013-04-19 15:09:34 +00:00
// Check if script isn't already running
2014-09-09 12:52:56 +00:00
if ( file_exists ( ALTERNC_DO_ACTION_LOCK ) !== false ){
2013-04-23 13:32:37 +00:00
d ( " Lock file already exists. " );
2013-04-19 15:09:34 +00:00
// Check if file is in process list
2014-09-09 12:52:56 +00:00
$PID = file_get_contents ( ALTERNC_DO_ACTION_LOCK );
2013-04-23 13:32:37 +00:00
d ( " My PID is $MY_PID , PID in the lock file is $PID " );
if ( $PID == exec ( " pidof $SCRIPT | tr ' ' ' \n ' | grep -v $MY_PID " )){
2013-04-19 15:09:34 +00:00
// Previous cron is not finished yet, just exit
2013-04-23 13:32:37 +00:00
d ( " Previous cron is already running, we just exit and let it finish :-) " );
2013-04-23 08:06:44 +00:00
exit ( 0 );
2013-04-19 15:09:34 +00:00
} else {
// Previous cron failed!
2014-09-09 12:52:56 +00:00
$errorsList [] = " Lock file already exists. No process with PID $PID found! Previous cron failed... \n " ;
2013-12-11 14:54:14 +00:00
// No need to remove anything, we're going to recreate it
//d("Removing lock file and trying to process the failed action...");
2013-04-19 15:09:34 +00:00
// Delete the lock and continue to the next action
2014-09-09 12:52:56 +00:00
//unlink(ALTERNC_DO_ACTION_LOCK);
2013-04-19 15:09:34 +00:00
2013-04-23 13:32:37 +00:00
// Lock with the current script's PID
2014-09-09 12:52:56 +00:00
if ( file_put_contents ( ALTERNC_DO_ACTION_LOCK , $MY_PID ) === false ){
$errorsList [] = " Cannot open/write ALTERNC_DO_ACTION_LOCK \n " ;
2013-04-25 13:05:27 +00:00
mail_it ();
exit ( 1 );
2013-04-23 13:32:37 +00:00
}
2013-04-23 15:00:23 +00:00
// Get the action(s) that was processing when previous script failed
// (Normally, there will be at most 1 job pending... but who know?)
2013-04-23 13:32:37 +00:00
while ( $cc = $action -> get_job ()){
$c = $cc [ 0 ];
2013-04-24 10:00:54 +00:00
$params = unserialize ( $c [ " parameters " ]);
2013-04-23 13:32:37 +00:00
// We can resume these types of action, so we reset the job to process it later
d ( " Previous job was the n° " . $c [ " id " ] . " : ' " . $c [ " type " ] . " ' " );
2014-03-07 12:48:16 +00:00
if ( $c [ " type " ] == " CREATE_FILE " && is_dir ( dirname ( $params [ " file " ])) || $c [ " type " ] == " CREATE_DIR " || $c [ " type " ] == " DELETE " || $c [ " type " ] == " FIX_DIR " || $c [ " type " ] == " FIX_FILE " ){
2013-04-23 13:32:37 +00:00
d ( " Reset of the job! So it will be resumed... " );
$action -> reset_job ( $c [ " id " ]);
} else {
// We can't resume the others types, notify the fail and finish this action
2014-09-09 12:52:56 +00:00
$errorsList [] = " Can't resume the job n° " . $c [ " id " ] . " action ' " . $c [ " type " ] . " ', finishing it with a fail status. \n " ;
2013-04-23 13:32:37 +00:00
if ( ! $action -> finish ( $c [ " id " ], " Fail: Previous script crashed while processing this action, cannot resume it. " )){
2014-09-09 12:52:56 +00:00
$errorsList [] = " Cannot finish the action! Error while inserting the error value in the DB for action n° " . $c [ " id " ] . " : action ' " . $c [ " type " ] . " ' \n " ;
2013-04-23 13:32:37 +00:00
break ; // Else we go into an infinite loop... AAAAHHHHHH
}
}
}
}
} else {
// Lock with the current script's PID
2014-09-09 12:52:56 +00:00
if ( file_put_contents ( ALTERNC_DO_ACTION_LOCK , $MY_PID ) === false ){
$errorsList [] = " Cannot open/write ALTERNC_DO_ACTION_LOCK \n " ;
2013-04-25 13:05:27 +00:00
mail_it ();
exit ( 1 );
2013-04-23 13:32:37 +00:00
}
2013-04-19 15:09:34 +00:00
}
//We get the next action to do
2013-04-23 08:06:44 +00:00
while ( $rr = $action -> get_action ()){
$r = $rr [ 0 ];
$return = " OK " ;
2013-04-25 13:05:27 +00:00
// Do we have to do this action with a specific user?
if ( $r [ " user " ] != " root " )
$SU = " su " . $r [ " user " ] . " 2>&1 ; " ;
else
$SU = " " ;
2013-04-19 15:09:34 +00:00
// We lock the action
2013-04-23 13:32:37 +00:00
d ( " ----------- \n Beginning action n° " . $r [ " id " ]);
2013-04-19 15:14:34 +00:00
$action -> begin ( $r [ " id " ]);
2013-04-19 15:09:34 +00:00
// We process it
2013-04-26 15:38:28 +00:00
$params =@ unserialize ( $r [ " parameters " ]);
2013-04-19 15:09:34 +00:00
// We exec with the specified user
2013-04-23 13:32:37 +00:00
d ( " Executing action ' " . $r [ " type " ] . " ' with user ' " . $r [ " user " ] . " ' " );
2013-04-19 15:09:34 +00:00
switch ( $r [ " type " ]){
2013-08-08 15:56:09 +00:00
case " FIX_USER " :
// Create the directory and make parent directories as needed
2014-03-07 14:57:55 +00:00
$returned = execute_cmd ( " $FIXPERM -u " , $params [ " uid " ]);
2014-09-09 12:52:56 +00:00
break ;
case " CHMOD " :
2014-09-29 15:06:52 +00:00
$filename = $params [ " filename " ];
2014-09-09 12:52:56 +00:00
$perms = $params [ " perms " ];
// Checks the file or directory exists
if ( ! is_dir ( $filename ) && ! is_file ( $filename )){
2014-09-29 15:06:52 +00:00
$errorsList = array ( " Fail: cannot retrieve CHMOD filename " );
2014-09-09 12:52:56 +00:00
}
// Checks the perms are correct
else if ( ! is_int ( $perms )){
2014-09-29 15:06:52 +00:00
$errorsList = array ( " Fail: Incorrect perms : $perms " );
2014-09-09 12:52:56 +00:00
}
// Attempts to change the rights on the file or directory
else if ( ! chmod ( $filename , $perms )) {
2014-09-29 15:06:52 +00:00
$errorsList = array ( " Fail: cannot change perms ( $perms ) on filename ( $filename ) " );
2014-09-09 12:52:56 +00:00
}
2013-08-08 15:56:09 +00:00
break ;
2013-04-19 15:09:34 +00:00
case " CREATE_FILE " :
2014-03-07 14:57:55 +00:00
if ( ! file_exists ( $params [ " file " ])) {
if ( file_put_contents ( $params [ " file " ], $params [ " content " ]) === false ) {
2014-09-09 12:52:56 +00:00
$errorsList = array ( " Fail: can't write into file " . $params [ " file " ]);
2014-03-07 14:57:55 +00:00
} else {
if ( ! chown ( $params [ " file " ], $r [ " user " ])) {
2014-09-09 12:52:56 +00:00
$errorsList = array ( " Fail: cannot chown " . $params [ " file " ]);
2014-03-07 14:57:55 +00:00
}
}
} else {
2014-09-09 12:52:56 +00:00
$errorsList = array ( " Fail: file already exists " . $params [ " file " ]);
2014-03-07 14:57:55 +00:00
}
2013-04-19 15:09:34 +00:00
break ;
case " CREATE_DIR " :
2013-04-23 13:32:37 +00:00
// Create the directory and make parent directories as needed
2014-03-07 14:57:55 +00:00
$returned = execute_cmd ( " $SU mkdir " , array ( '-p' , $params [ " dir " ]));
2013-04-19 15:09:34 +00:00
break ;
case " DELETE " :
2013-04-23 13:32:37 +00:00
// Delete file/directory and its contents recursively
2014-03-07 14:57:55 +00:00
$returned = execute_cmd ( " $SU rm " , array ( '-rf' , $params [ " dir " ]));
2013-04-19 15:09:34 +00:00
break ;
case " MOVE " :
2013-04-23 08:06:44 +00:00
// If destination dir does not exists, create it
if ( ! is_dir ( $params [ " dst " ]))
2014-03-07 14:57:55 +00:00
if ( @ mkdir ( $params [ " dst " ], 0777 , true )) {
if ( @ chown ( $params [ " dst " ], $r [ " user " ]) ) {
$returned = execute_cmd ( " $SU mv -f " , array ( $params [ " src " ], $params [ " dst " ]));
}
} else { //is_dir false
2014-09-09 12:52:56 +00:00
$errorsList = array ( " Fail: cannot create " . $params [ " dst " ]);
2014-03-07 14:57:55 +00:00
} // is_dir
2013-04-19 15:09:34 +00:00
break ;
2014-03-07 12:48:16 +00:00
case " FIX_DIR " :
2014-03-07 14:57:55 +00:00
$returned = execute_cmd ( $FIXPERM , array ( '-d' , $params [ " dir " ]));
2014-09-09 12:52:56 +00:00
if ( $returned [ 'return_val' ] != 0 ) {
$errorsList = array ( " Fixperms.sh failed, returned error code : " . $returned [ 'return_val' ]);
}
2013-04-26 15:01:49 +00:00
break ;
2014-03-07 12:48:16 +00:00
case " FIX_FILE " :
2014-03-07 14:57:55 +00:00
$returned = execute_cmd ( $FIXPERM , array ( '-f' , $params [ " file " ]));
2014-09-09 12:52:56 +00:00
if ( $returned [ 'return_val' ] != 0 ){
$errorsList = array ( " Fixperms.sh failed, returned error code : " . $returned [ 'return_val' ]);
}
2013-04-22 08:06:48 +00:00
break ;
2013-04-19 15:09:34 +00:00
default :
2013-04-24 10:00:54 +00:00
$output = array ( " Fail: Sorry dude, i do not know this type of action " );
2013-04-19 15:09:34 +00:00
break ;
}
2013-04-25 13:05:27 +00:00
// Get the error (if exists).
if ( isset ( $output [ 0 ])){
2013-04-23 08:06:44 +00:00
$return = $output [ 0 ];
2014-09-09 12:52:56 +00:00
$errorsList [] = " \n Action n° " . $r [ " id " ] . " ' " . $r [ " type " ] . " ' failed! With user: " . $r [ " user " ] . " \n Here is the complete output: \n " . print_r ( $output );
2013-04-25 13:05:27 +00:00
}
2013-04-23 08:06:44 +00:00
// We finished the action, notify the DB.
2013-04-23 13:32:37 +00:00
d ( " Finishing... return value is : $return\n " );
if ( ! $action -> finish ( $r [ " id " ], addslashes ( $return ))){
2014-09-09 12:52:56 +00:00
$errorsList [] = " Cannot finish the action! Error while inserting the error value in the DB for action n° " . $r [ " id " ] . " : action ' " . $r [ " type " ] . " ' \n Return value: " . addslashes ( $return ) . " \n " ;
2013-04-23 13:32:37 +00:00
break ; // Else we go into an infinite loop... AAAAHHHHHH
}
2013-04-19 15:09:34 +00:00
}
2014-09-09 12:52:56 +00:00
// If an error occured, notify it to the admin
if ( count ( $errorsList )) {
2014-09-29 15:06:52 +00:00
mail_it ();
if ( ( php_sapi_name () === 'cli' ) ){
echo _ ( " errors were met " );
var_dump ( $errorsList );
}
2014-03-07 14:57:55 +00:00
}
2013-04-25 13:05:27 +00:00
2013-04-19 15:09:34 +00:00
// Unlock the script
2014-09-09 12:52:56 +00:00
// @todo This could be handled by m_admin
unlink ( ALTERNC_DO_ACTION_LOCK );
2013-04-26 08:11:59 +00:00
2013-04-23 08:06:44 +00:00
// Exit this script
2014-09-29 15:06:52 +00:00
exit ( 0 );