function inject_extracted_attach($filepath) { if ($GLOBALS["VERBOSE"]) { echo "Injecting {$filepath}\n"; } $unix = new unix(); $MYSQL_DATA_DIR = $unix->MYSQL_DATA_DIR(); if (!is_dir($MYSQL_DATA_DIR)) { @mkdir($MYSQL_DATA_DIR, 0755, true); @chown($MYSQL_DATA_DIR, "mysql"); @chgrp($MYSQL_DATA_DIR, "mysql"); } $basename = basename($filepath); $tempfile = "{$MYSQL_DATA_DIR}/{$basename}"; $last_modified = filemtime($filepath); $filetime = date("Y-m-d H:i:s", $last_modified); $filesize = $unix->file_size($filepath); @copy($filepath, $tempfile); @chmod($tempfile, 0777); $q = new mysql_mimedefang_builder(); $sql = "SELECT `filename` FROM `storage` WHERE `filename`='{$basename}'"; $ligne = mysql_fetch_array($q->QUERY_SQL($sql)); if (strlen(trim($ligne["filename"])) > 0) { if ($GLOBALS["VERBOSE"]) { echo "Skipped {$basename}, already exists in database\n"; } @unlink($filepath); return; } $sql = "INSERT INTO `storage` (`filename`,`filetime`,`filesize`,`filedata`) VALUES ('{$basename}','{$filetime}','{$filesize}',LOAD_FILE('{$filepath}'))"; $q->QUERY_SQL($sql); if (!$q->ok) { system_admin_events("Fatal: {$q->mysql_error}.", __FUNCTION__, __FILE__, __LINE__, "postfix"); $returned = false; } else { $returned = true; @unlink($filepath); } @unlink($tempfile); return $returned; }
function streamget() { $sock = new sockets(); $unix = new unix(); $SquidGuardStorageDir = $sock->GET_INFO("SquidGuardStorageDir"); $EnableRemoteStatisticsAppliance = $sock->GET_INFO("EnableRemoteStatisticsAppliance"); if (!is_numeric($EnableRemoteStatisticsAppliance)) { $EnableRemoteStatisticsAppliance = 0; } $hostname = $unix->FULL_HOSTNAME(); $PREFIX = "INSERT IGNORE INTO `youtubecache`(`filename`,`filesize`,`urlsrc`,`zDate`,`zMD5`,`proxyname`) VALUES "; $q = new mysql_squid_builder(); if (!($handle = opendir($SquidGuardStorageDir))) { events_tail("streamget:: -> glob failed {$SquidGuardStorageDir} in Line: " . __LINE__); return; } while (false !== ($filename = readdir($handle))) { if ($filename == ".") { continue; } if ($filename == "..") { continue; } $fullFileName = "{$SquidGuardStorageDir}/{$filename}"; $time = null; if (strpos($filename, ".url") > 0) { continue; } if (strpos($filename, ".log") > 0) { continue; } $filesize = $unix->file_size($fullFileName); $time = filemtime($fullFileName); $zdate = date("Y-m-d H:i:s", $time); $url = null; if (is_file($fullFileName . ".url")) { $url = @file_get_contents($fullFileName . ".url"); } if ($GLOBALS["VERBOSE"]) { echo "\n\nFile:{$fullFileName}\nSize:{$filesize}\ndate:{$zdate}\nurl:{$url}\n"; } $md5 = md5($filename . $hostname); $f[] = "('{$fullFileName}','{$filesize}','{$url}','{$zdate}','{$md5}','{$hostname}')"; } if (count($f) > 0) { $sql = $PREFIX . " " . @implode(",", $f); if ($EnableRemoteStatisticsAppliance == 0) { $q->QUERY_SQL($sql); if (!$q->ok) { events_tail("streamget:: Fatal {$q->mysql_error}"); } } else { if ($GLOBALS["VERBOSE"]) { echo "streamget_send_remote() with hostname {$hostname}\n"; } streamget_send_remote($sql, $hostname); } } }
function ScanUpdateUtilityLogs($force = false) { $unix = new unix(); $sock = new sockets(); $unix = new unix(); $timefile = "/etc/artica-postfix/pids/ScanUpdateUtilityLogs.time"; $pidfile = "/etc/artica-postfix/pids/ScanUpdateUtilityLogs.pid"; if (!$force) { $pid = $unix->get_pid_from_file($pidfile); if ($unix->process_exists($pid, basename(__FILE__))) { $time = $unix->PROCCESS_TIME_MIN($pid); if ($GLOBALS["VERBOSE"]) { echo "Already process exists {$pid}\n"; } return; } @file_put_contents($pidfile, getmypid()); $time = $unix->file_time_min($timefile); if ($timefile < 10) { if ($GLOBALS["VERBOSE"]) { echo "Only each 10mn\n"; } return; } } @unlink($timefile); @file_put_contents($timefile, time()); $UpdateUtilityStorePath = $sock->GET_INFO("UpdateUtilityStorePath"); if ($UpdateUtilityStorePath == null) { $UpdateUtilityStorePath = "/home/kaspersky/UpdateUtility"; } $UpdateUtilityUseLoop = $sock->GET_INFO("UpdateUtilityUseLoop"); if (!is_numeric($UpdateUtilityUseLoop)) { $UpdateUtilityUseLoop = 0; } if ($UpdateUtilityUseLoop == 1) { $UpdateUtilityStorePath = "/automounts/UpdateUtility"; } if ($GLOBALS["VERBOSE"]) { echo "Scanning /var/log/artica-postfix/UpdateUtility-*.log...\n"; } foreach (glob("/var/log/artica-postfix/UpdateUtility-*.log") as $filename) { $timefile = $unix->file_time_min($filename); $time = filemtime($filename); $details = @file_get_contents($filename); $f = explode("\n", $details); $isSuccess = 1; $files = 0; $size = 0; $rp_finish = false; if ($timefile > 720) { $rp_finish = true; } while (list($key, $line) = each($f)) { if (preg_match("#New file installed '(.*?)'#", $line, $re)) { $nextFile = "{$UpdateUtilityStorePath}/databases/Updates/{$re[1]}"; if (!is_file($nextFile)) { if ($GLOBALS["VERBOSE"]) { echo "{$nextFile}, no such file\n"; } $nextFile = "{$UpdateUtilityStorePath}/TempFolder/temporaryFolder/{$re[1]}"; } if (!is_file($nextFile)) { if ($GLOBALS["VERBOSE"]) { echo "{$nextFile}, no such file\n"; } continue; } $files++; $size = $size + $unix->file_size($nextFile); continue; } if (preg_match("#Segmentation fault#", $line)) { $isSuccess = 0; continue; $rp_finish = true; } if (preg_match("#Bus error#", $line)) { $isSuccess = 0; continue; $rp_finish = true; } if (preg_match("#Report finished at#", $line, $re)) { $rp_finish = true; continue; } if (preg_match("#Insufficient disk space#i", $line, $re)) { $isSuccess = 0; continue; $rp_finish = true; } if (preg_match("#Failed to#i", $line, $re)) { $isSuccess = 0; continue; } if (preg_match("#not retranslated#i", $line, $re)) { $isSuccess = 0; continue; $rp_finish = true; } if (preg_match("#Retranslation operation result 'Success'#i", $line, $re)) { $isSuccess = 1; continue; } } if (!$rp_finish) { if ($GLOBALS["VERBOSE"]) { echo "Not finished {$timefile}Mn/720Mn {$filename}\n"; } continue; } $date = date("Y-m-d H:i:s", $time); if (preg_match("#UpdateUtility-.*?([0-9]+)\\.log\$#", basename($filename), $re)) { $date = date("Y-m-d H:i:s", $re[1]); } echo "{$date} {$files} downloaded {$size} bytes\n"; $q = new mysql(); $details = mysql_escape_string2($details); $q->QUERY_SQL("INSERT INTO updateutilityev (`zDate`,`filesize`,`filesnum`,`details`,`isSuccess`) \n\t\t\t\tVALUES ('{$date}','{$files}','{$size}','{$details}','{$isSuccess}')", "artica_events"); if (!$q->ok) { continue; } @unlink($filename); continue; } }
function replic_host($servername) { $t = time(); $unix = new unix(); $free = new freeweb($servername); $instanceid = $free->mysql_instance_id; $localdatabase = $free->mysql_database; if (!isset($free->Params["ROUNDCUBE"]["ENABLE_REPLIC"])) { if ($GLOBALS["VERBOSE"]) { echo "{$servername}: ROUNDCUBE/ENABLE_REPLIC no set\n"; } return null; } if ($free->Params["ROUNDCUBE"]["ENABLE_REPLIC"] == 0) { if ($GLOBALS["VERBOSE"]) { echo "{$servername}: ROUNDCUBE/ENABLE_REPLIC set to disabled\n"; } return null; } $ARTICA_PORT = $free->Params["ROUNDCUBE"]["ARTICA_PORT"]; $ARTICA_ADMIN = $free->Params["ROUNDCUBE"]["ARTICA_ADMIN"]; $ARTICA_PASSWORD = $free->Params["ROUNDCUBE"]["ARTICA_PASSWORD"]; $ARTICA_HOST = $free->Params["ROUNDCUBE"]["ARTICA_HOST"]; $ARTICA_RMWEB = $free->Params["ROUNDCUBE"]["ARTICA_RMWEB"]; if ($GLOBALS["VERBOSE"]) { echo "Send order to get database dump {$ARTICA_HOST}:{$ARTICA_PORT}\n"; } $auth = array("username" => $ARTICA_ADMIN, "password" => md5($ARTICA_PASSWORD)); $auth = base64_encode(serialize($auth)); $curl = new ccurl("https://{$ARTICA_HOST}:{$ARTICA_PORT}/exec.gluster.php"); $curl->noproxyload = true; $curl->parms["AUTH"] = $auth; $curl->parms["RDCUBE-REPLIC"] = $ARTICA_RMWEB; if (!$curl->get()) { if ($GLOBALS["VERBOSE"]) { echo "Error replicate roundcube to {$ARTICA_HOST}:{$ARTICA_PORT} with error {$curl->error}\n"; } system_admin_events("Error replicate roundcube to {$ARTICA_HOST}:{$ARTICA_PORT} with error {$curl->error}", __FUNCTION__, __FILE__, __LINE__, "roundcube"); return; } preg_match("#<INFOS>(.*?)</INFOS>#is", $curl->data, $re); if ($GLOBALS["VERBOSE"]) { echo "{$curl->data}\n"; } if (!preg_match("#<FILENAME>(.*?)</FILENAME>#is", $curl->data, $re)) { preg_match("#<ERROR>(.*?)</ERROR>#is", $curl->data, $re); if ($GLOBALS["VERBOSE"]) { echo "Error replicate roundcube to {$ARTICA_HOST}:{$ARTICA_PORT} with error {$re[1]}\n"; } system_admin_events("Error replicate roundcube to {$ARTICA_HOST}:{$ARTICA_PORT} with error {$re[1]}", __FUNCTION__, __FILE__, __LINE__, "roundcube"); return; } $filepath = $re[1]; $filename = basename($filepath); $curl = new ccurl("https://{$ARTICA_HOST}:{$ARTICA_PORT}/{$filepath}"); if (!$curl->GetFile("/tmp/{$filename}")) { if ($GLOBALS["VERBOSE"]) { echo "Error get roundcube database from {$filepath} with error {$curl->error}\n"; } system_admin_events("Error get roundcube database from {$filepath} with error {$curl->error}", __FUNCTION__, __FILE__, __LINE__, "roundcube"); return; } $filesize = $unix->file_size("/tmp/{$filename}"); if ($GLOBALS["VERBOSE"]) { echo "Downloading {$filename} done with {$filesize} bytes\n"; } if (!$unix->uncompress("/tmp/{$filename}", "/tmp/{$filename}.sql")) { @unlink("/tmp/{$filename}"); if ($GLOBALS["VERBOSE"]) { echo "Error uncompress {$filepath}\n"; } system_admin_events("Error uncompress {$filepath}", __FUNCTION__, __FILE__, __LINE__, "roundcube"); return; } @unlink("/tmp/{$filename}"); $mysqlbin = $unix->find_program("mysql"); if ($instanceid > 0) { $q = new mysql_multi($instance_id); if ($q->mysql_password != null) { $password = "******"; } $cmdline = "{$mysqlbin} --batch --force --user={$q->mysql_admin}{$password} --socket={$q->SocketPath} --database={$localdatabase} </tmp/{$filename}.sql 2>&1"; } else { $q = new mysql(); if ($q->mysql_server == "127.0.0.1") { $servcmd = " --socket=/var/run/mysqld/mysqld.sock "; } else { $servcmd = " --host={$q->mysql_server} --port={$q->mysql_port} "; } if ($q->mysql_password != null) { $password = "******"; } $cmdline = "{$mysqlbin} --batch --force --user={$q->mysql_admin}{$password} {$servcmd} --database={$localdatabase} </tmp/{$filename}.sql 2>&1"; } if ($GLOBALS["VERBOSE"]) { echo $cmdline . "\n"; } shell_exec($cmdline); $took = $unix->distanceOfTimeInWords($t, time(), true); system_admin_events("Success import from {$filename} to {$localdatabase} took {$took}", __FUNCTION__, __FILE__, __LINE__, "roundcube"); @unlink("/tmp/{$filename}.sql"); }
function Dumpimport($innodb_file_pertable = 0) { system_admin_events("Create pointer to block Artica ", __FUNCTION__, __FILE__, __LINE__, "mysql"); @mkdir("/home/mysqldump", 0755, true); $tmpfile = "/home/mysqldump/all-database.sql"; $tmpError = "/home/mysqldump/all-database.errors"; $t1 = time(); $sock = new sockets(); $sock->SET_INFO("DisableMySqlTemp", 0); $unix = new unix(); $q = new mysql(); $mysql_admin = $q->mysql_admin; $MYSQL_DATA_DIR = $unix->MYSQL_DATA_DIR(); if (trim($mysql_admin) == null) { system_admin_events("Error, unable to get mysql_admin credentials", __FUNCTION__, __FILE__, __LINE__, "mysql"); die; } if (!is_file($tmpfile)) { system_admin_events("Error, {$tmpfile} no such file", __FUNCTION__, __FILE__, __LINE__, "mysql"); die; } $password = $q->mysql_password; if ($password != null) { $passwordcmdline = " --password={$password}"; } $mysqlbin = $unix->find_program("mysql"); if (!is_file($mysqlbin)) { system_admin_events("Error, mysqldump no such binary, aborting", __FUNCTION__, __FILE__, __LINE__, "mysql"); die; } $BaseDir = $MYSQL_DATA_DIR; $files = $unix->DirFiles($BaseDir); system_admin_events("Info, stopping mysql", __FUNCTION__, __FILE__, __LINE__, "mysql"); @file_put_contents("/etc/artica-postfix/mysql.stop", time()); shell_exec("/etc/init.d/artica-postfix stop mysql"); while (list($index, $line) = each($files)) { $fsize = FormatBytes($unix->file_size("{$BaseDir}/{$line}")); if (preg_match("#ib_logfile#", $line)) { system_admin_events("Info, removing {$BaseDir}/{$line}", __FUNCTION__, __FILE__, __LINE__, "mysql"); @unlink("/{$BaseDir}/{$line}"); $deleted[] = $line . ": {$fsize}"; continue; } if (preg_match("#ibdata#", $line)) { system_admin_events("Info, removing {$BaseDir}/{$line}", __FUNCTION__, __FILE__, __LINE__, "mysql"); @unlink("/{$BaseDir}/{$line}"); $deleted[] = $line . ": {$fsize}"; continue; } $skipped[] = $line . ": {$fsize}"; } if ($innodb_file_pertable == 1) { system_admin_events("Info, innodb_file_per_table is enabled", __FUNCTION__, __FILE__, __LINE__, "mysql"); $sock->SET_INFO("InnodbFilePerTable", 1); $php5 = $unix->LOCATE_PHP5_BIN(); shell_exec("{$php5} " . dirname(__FILE__) . "/exec.mysql.build.php"); system_admin_events("Deleting, ibdata1,ib_logfile0,ib_logfile1 is enabled", __FUNCTION__, __FILE__, __LINE__, "mysql"); @unlink("{$MYSQL_DATA_DIR}/ibdata1"); @unlink("{$MYSQL_DATA_DIR}/ib_logfile0"); @unlink("{$MYSQL_DATA_DIR}/ib_logfile1"); } @unlink("/etc/artica-postfix/mysql.stop"); system_admin_events("Info, Cleaning Mysql directory: delete files\n" . @implode($deleted, "\n") . "Skipped files:\n" . @implode($skipped, "\n"), __FUNCTION__, __FILE__, __LINE__, "mysql"); system_admin_events("Info, starting mysql", __FUNCTION__, __FILE__, __LINE__, "mysql"); exec("/etc/init.d/artica-postfix start mysql 2>&1", $results); system_admin_events("Info, starting mysql done\n" . @implode("\n", $results), __FUNCTION__, __FILE__, __LINE__, "mysql"); unset($results); system_admin_events("Stamp artica to not trying to inject to mysql", __FUNCTION__, __FILE__, __LINE__, "mysql"); sleep(2); for ($i = 0; $i < 50; $i++) { $q = new mysql(); $sleep = true; if (!$q->DATABASE_EXISTS("artica_backup")) { $sleep = true; } $pid = @file_get_contents("/var/run/mysqld/mysqld.pid"); if ($unix->process_exists($pid)) { echo "MySQL server running PID {$pid}\n"; $sleep = false; } else { $sleep = true; } if ($sleep) { echo "Sleeping 1s pid:{$pid} not running, artica_backup not available...\n"; continue; } break; } if (!$unix->process_exists($pid)) { $pid = @file_get_contents("/var/run/mysqld/mysqld.pid"); echo "MySQL server not running...\n"; system_admin_events("Task aborted, MySQL did not running...", __FUNCTION__, __FILE__, __LINE__, "mysql"); return; } $t = time(); @unlink($tmpError); $cmd = "{$mysqlbin} --batch --force --user={$mysql_admin}{$passwordcmdline} < {$tmpfile} >{$tmpError} 2>&1"; if ($GLOBALS["VERBOSE"]) { echo "{$cmd}\n"; } shell_exec($cmd); $sock->SET_INFO("DisableMySqlTemp", 0); $t2 = time(); $took = $unix->distanceOfTimeInWords($t, $t2, true); system_admin_events("finish restoring all databases and tables took: {$took}", __FUNCTION__, __FILE__, __LINE__, "mysql"); $ISERRORED = false; while (list($index, $line) = each($f)) { if (preg_match("#error:\\s+([0-9]+)#i", $line)) { system_admin_events("Failed with error {$line}", __FUNCTION__, __FILE__, __LINE__, "mysql"); $ISERRORED = true; } if (preg_match("#ERROR\\s+([0-9]+)#i", $line)) { system_admin_events("Failed with error {$line}", __FUNCTION__, __FILE__, __LINE__, "mysql"); $ISERRORED = true; } if ($GLOBALS["VERBOSE"]) { echo "\"{$line}\" Unknown....line\n"; } } $took = $unix->distanceOfTimeInWords($t1, time(), true); if (!$ISERRORED) { system_admin_events("importing the entire Mysql done took:{$took}", __FUNCTION__, __FILE__, __LINE__, "mysql"); @unlink($tmpfile); @unlink($tmpError); } else { system_admin_events("Procedure Failed the mysqldump file is skipped in {$tmpfile} path, you can retry yourself to import datas using this commandline:\n{$cmd}", __FUNCTION__, __FILE__, __LINE__, "mysql"); } $unix = new unix(); $zaraf = $unix->find_program("zarafa-server"); if (is_file($zaraf)) { shell_exec("/etc/init.d/artica-postfix restart zarafa-server"); } }
function UFDBGUARD_STATUS() { $Narray = array(); $unix = new unix(); $array = explode("\n", @file_get_contents("/etc/ufdbguard/ufdbGuard.conf")); while (list($index, $line) = each($array)) { if (preg_match("#domainlist.+?(.+)\\/domains#", $line, $re)) { $datas_path = "/var/lib/squidguard/{$re[1]}/domains"; $path = "/var/lib/squidguard/{$re[1]}/domains.ufdb"; $size = $unix->file_size($path); $Narray[$path] = $size; } } @file_put_contents("/usr/share/artica-postfix/ressources/logs/ufdbguard.db.size.txt", serialize($Narray)); chmod("/usr/share/artica-postfix/ressources/logs/ufdbguard.db.size.txt", 777); return $Narray; }
function SmtpNotificationConfigRead() { $unix = new unix(); $size = $unix->file_size("/etc/artica-postfix/smtpnotif.conf"); $datas = trim(@file_get_contents("/etc/artica-postfix/smtpnotif.conf")); echo "<articadatascgi>{$datas}</articadatascgi>"; }
function UFDBGUARD_STATUS() { $sock = new sockets(); $EnableRemoteStatisticsAppliance = $sock->GET_INFO("EnableRemoteStatisticsAppliance"); if (!is_numeric($EnableRemoteStatisticsAppliance)) { $EnableRemoteStatisticsAppliance = 0; } if ($EnableRemoteStatisticsAppliance == 1) { return; } $Narray = array(); $unix = new unix(); $array = explode("\n", @file_get_contents("/etc/ufdbguard/ufdbGuard.conf")); while (list($index, $line) = each($array)) { if (preg_match("#domainlist.+?(.+)\\/domains#", $line, $re)) { $datas_path = "/var/lib/squidguard/{$re[1]}/domains"; $path = "/var/lib/squidguard/{$re[1]}/domains.ufdb"; $size = $unix->file_size($path); $Narray[$path] = $size; } } @file_put_contents("/usr/share/artica-postfix/ressources/logs/ufdbguard.db.size.txt", serialize($Narray)); chmod("/usr/share/artica-postfix/ressources/logs/ufdbguard.db.size.txt", 777); return $Narray; }
function SafeBoxIncreaseSize($uid) { $unix = new unix(); $safe = new safebox($uid); $mapper = "/dev/mapper/{$uid}"; $SafeBox = $safe->crypted_filepath; $file_size = $unix->file_size($SafeBox) / 1024; $file_size = $file_size / 1000; $file_size = $file_size / 1000; $file_size = round($file_size); $increase = $safe->CryptedHomeSize - $file_size; writelogs("{$SafeBox} ({$uid}) {$file_size}Go increase it to {$increase}Go", __FUNCTION__, __FILE__, __LINE__); if ($increase < 0) { writelogs("Failed negative size", __FUNCTION__, __FILE__, __LINE__); return; } $cmd = $unix->find_program("dd") . " conv=notrunc oflag=append if=/dev/zero bs=1G count={$increase} of={$SafeBox}"; exec($cmd, $results); logsArray($results, __FUNCTION__, __LINE__); if (!mountLoop($SafeBox)) { writelogs("Failed to mount loop", __FUNCTION__, __FILE__, __LINE__); return false; } if (!_CryptOpen($SafeBox, $GLOBALS["CRYPT_PASSWORD"])) { writelogs("Failed to open {$SafeBox}", __FUNCTION__, __FILE__, __LINE__); } $cmd = $unix->find_program("cryptsetup") . " resize {$uid}"; writelogs("{$cmd}", __FUNCTION__, __FILE__, __LINE__); exec($cmd, $results); logsArray($results, __FUNCTION__, __LINE__); $cmd = $unix->find_program("e2fsck") . " -f /dev/mapper/{$uid}"; writelogs("{$cmd}", __FUNCTION__, __FILE__, __LINE__); exec($cmd, $results); logsArray($results, __FUNCTION__, __LINE__); $cmd = $unix->find_program("resize2fs") . " -p /dev/mapper/{$uid}"; writelogs("{$cmd}", __FUNCTION__, __FILE__, __LINE__); exec($cmd, $results); logsArray($results, __FUNCTION__, __LINE__); return true; }
function check_all_squid() { $sock = new sockets(); $unix = new unix(); $syslog = new mysql_storelogs(); $php5 = $unix->LOCATE_PHP5_BIN(); $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid"; $timefile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".time"; $pid = file_get_contents("{$pidfile}"); if (system_is_overloaded(basename(__FILE__))) { die; } if ($unix->process_exists($pid, basename(__FILE__))) { $timeMin = $unix->PROCCESS_TIME_MIN($pid); if ($timeMin > 240) { system_admin_events("Too many TTL, {$pid} will be killed", __FUNCTION__, __FILE__, __LINE__, "logrotate"); $kill = $unix->find_program("kill"); unix_system_kill_force($pid); } else { die; } } $time = $unix->file_time_min($timefile); if ($time < 300) { return; } @file_put_contents($pidfile, getmypid()); @file_put_contents($timefile, time()); $php = $unix->LOCATE_PHP5_BIN(); $bzip2 = $unix->find_program("bzip2"); $ALREADYCOMP["gz"] = true; $ALREADYCOMP["bz2"] = true; $LogRotateCompress = 1; $LogRotatePath = $sock->GET_INFO("LogRotatePath"); $ApacheLogRotate = $sock->GET_INFO("ApacheLogRotate"); if (!is_numeric($ApacheLogRotate)) { $ApacheLogRotate = 1; } if ($LogRotatePath == null) { $LogRotatePath = "/home/logrotate"; } $LogsRotateDefaultSizeRotation = $sock->GET_INFO("LogsRotateDefaultSizeRotation"); if (!is_numeric($LogsRotateDefaultSizeRotation)) { $LogsRotateDefaultSizeRotation = 100; } foreach (glob("/var/log/squid/*") as $filename) { if (is_dir($filename)) { continue; } $size = $unix->file_size($filename); $time = $unix->file_time_min($filename); $size = round($size / 1024 / 1000, 2); if ($size > $LogsRotateDefaultSizeRotation) { if ($filename == "/var/log/squid/access.log") { events("{$filename} -> is a production log for Squid, launch the rotation procedure."); squid_admin_mysql(1, "{$filename} {$size}M exceed {$LogsRotateDefaultSizeRotation}M, launch rotation", null, __FILE__, __LINE__); shell_exec("{$php} /usr/share/artica-postfix/exec.squid.php --rotate"); continue; } $TOROT[$filename] = true; events("{$filename} -> Add to queue {$size}M exceed {$LogsRotateDefaultSizeRotation}M"); continue; } if ($time > 1440) { if ($filename == "/var/log/squid/access.log") { events("{$filename} -> is a production log for Squid, launch the rotation procedure."); squid_admin_mysql(1, "{$filename} {$size}M exceed {$LogsRotateDefaultSizeRotation}M, launch rotation", null, __FILE__, __LINE__); shell_exec("{$php} /usr/share/artica-postfix/exec.squid.php --rotate"); continue; } events("{$filename} -> Add to queue {$time}mn exceed 1440mn"); $TOROT[$filename] = true; continue; } } if (count($TOROT) == 0) { return; } while (list($filename, $none) = each($TOROT)) { $extension = pathinfo($filename, PATHINFO_EXTENSION); $filedate = date('Y-m-d H:i:s', filemtime($filename)); $basename = basename($filename); if (preg_match("#sarg\\.#", $filename)) { shell_exec("{$php5} " . dirname(__FILE__) . "/exec.sarg.php --rotate {$basename} >/dev/null 2>&1 &"); continue; } if (preg_match("#access\\.log\\.[0-9]+\$#", $filename)) { continue; } if ($extension != "gz") { if (!$unix->compress($filename, "{$filename}.gz")) { continue; } $filename = $filename . ".gz"; $extension = "gz"; } echo "[{$filedate}]: {$filename} ({$extension})\n"; if ($syslog->ROTATE_TOMYSQL($filename, $filedate)) { @unlink($filename); } } foreach (glob("/home/squid/cache-logs/*") as $filename) { $filedate = date('Y-m-d H:i:s', filemtime($filename)); $filename = $filename . ".gz"; if ($syslog->ROTATE_TOMYSQL($filename, $filedate)) { @unlink($filename); } } }
function backup_md5($md5) { $sock = new sockets(); $unix = new unix(); $q = new mysql(); $backend_root = "/root/.offlineimap"; $pidfile = "/var/run/offlineimap-{$md5}.pid"; $pid = @file_get_contents($pidfile); if ($unix->process_exists($pid)) { $timemin = $unix->PROCCESS_TIME_MIN($pid); $timefile = buildlogs("PID {$pid} running since {$timemin}mn , aborting", __FUNCTION__, __LINE__); LogsToMysqlMD5($md5); return; } $mypid = getmypid(); @file_put_contents($pidfile, $mypid); if ($GLOBALS["VERBOSE"]) { echo "{$pidfile} -> {$mypid}\n"; } $OfflineImapBackupTool = $sock->GET_INFO("OfflineImapBackupTool"); if (!is_numeric($OfflineImapBackupTool)) { $OfflineImapBackupTool = 0; } $sql = "SELECT * FROM mbxs_backup WHERE zmd5='{$md5}'"; $ligne = @mysql_fetch_array($q->QUERY_SQL($sql, 'artica_backup')); if (!$q->ok) { buildlogs("{$q->mysql_error}", __FUNCTION__, __LINE__); LogsToMysqlMD5($md5); return; } $uid = $ligne["uid"]; $account = $ligne["account"]; $imapserv = $ligne["imapserv"]; $MySqlQuery = $ligne; $OfflineImapWKDir = $sock->GET_INFO("OfflineImapWKDir"); $OfflineImapBackupDir = $sock->GET_INFO("OfflineImapBackupDir"); if ($OfflineImapBackupDir == null) { $OfflineImapBackupDir = "%HOME%/mailbackups"; } if ($OfflineImapWKDir == null) { $OfflineImapWKDir = "/home/artica/mailbackups"; } if ($OfflineImapBackupTool == 0) { buildlogs("This feature is disabled, aborting", __FUNCTION__, __LINE__); LogsToMysqlMD5($md5); return; } $fileZ = build_remote_backup_settings($MySqlQuery); $fileConf = $fileZ[0]; $TargetDir = $fileZ[1]; $logfile = "/var/log/" . basename($unix->FILE_TEMP()); if (!is_file($fileConf)) { LogsToMysqlMD5($md5); return; } if (!is_dir($TargetDir)) { buildlogs("{$TargetDir} no such directory", __FUNCTION__, __LINE__); LogsToMysqlMD5($md5); return; } $offlineimap = $unix->find_program("offlineimap"); $tar = $unix->find_program("tar"); $rm = $unix->find_program("rm"); $NICE = EXEC_NICE(); $t = time(); $CacheLocal = "{$backend_root}/Account-{$ligne["uid"]}"; $CacheRemote = "{$backend_root}/Account-{$ligne["account"]}"; if (is_dir($CacheLocal)) { shell_exec("{$rm} -rf {$CacheLocal}"); } if (is_dir($CacheRemote)) { shell_exec("{$rm} -rf {$CacheRemote}"); } $cmd = "{$NICE}{$offlineimap} -u basic -c {$fileConf} -l {$logfile} 2>&1"; exec($cmd, $results); if (count($results) > 0) { while (list($num, $ligne) = each($results)) { buildlogs($ligne, __FUNCTION__, __LINE__); } } $results = explode("\n", @file_get_contents($logfile)); if (count($results) > 0) { while (list($num, $ligne) = each($results)) { buildlogs($ligne, __FUNCTION__, __LINE__); } } @unlink($logfile); $offlimap = new offlineimap(); $FinalDir = $offlimap->maildir_parse($OfflineImapBackupDir, $uid); @mkdir($FinalDir, 0755, true); $FinalFile = "{$FinalDir}/" . date("YmdH") . "-{$account}@{$imapserv}.tar.bz2"; chdir($TargetDir); $cmd = "{$tar} -cjf {$FinalFile} *"; shell_exec($cmd); chdir("/root"); $cmd = "{$rm} -rf {$TargetDir}/"; shell_exec($cmd); $FinalFileSize = $unix->file_size_human($FinalFile); buildlogs("{$FinalFile}: {$FinalFileSize}", __FUNCTION__, __LINE__); buildlogs("Execution done took:" . $unix->distanceOfTimeInWords($t, time()), __FUNCTION__, __LINE__); LogsToMysqlMD5($md5); $imapserv = addslashes($imapserv); $account = addslashes($account); $FinalFileSize = $unix->file_size($FinalFile); $FinalFile = addslashes($FinalFile); $q->QUERY_SQL("DELETE FROM mbxs_backup_storage WHERE filepath='{$FinalFile}'", "artica_backup"); $sql = "INSERT IGNORE INTO mbxs_backup_storage (`zDate`,`filepath`,`filesize`,`imapserv`,`account`,`zmd5`) \n\tVALUES (NOW(),'{$FinalFile}','{$FinalFileSize}','{$imapserv}','{$account}','{$md5}')"; $q->QUERY_SQL($sql, "artica_backup"); }
function scan($aspid = false) { $unix = new unix(); $sock = new sockets(); $q = new mysql_squid_builder(); if (!$aspid) { $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid"; $pid = @file_get_contents($pidfile); if ($pid < 100) { $pid = null; } if ($unix->process_exists($pid, basename(__FILE__))) { $timepid = $unix->PROCCESS_TIME_MIN($pid); events("Already executed pid {$pid} since {$timepid}Mn"); return; } @file_put_contents($pidfile, time()); } if (!tests_nas()) { return; } $SquidOldLogsNAS = unserialize(base64_decode($sock->GET_INFO("SquidOldLogsNAS"))); $mountPoint = "/mnt/SquidImportLogs"; $NasFolder = $SquidOldLogsNAS["folder"]; $NasFolder = str_replace('\\', '/', $NasFolder); $NasFolder = str_replace('//', '/', $NasFolder); if (strpos($NasFolder, "/") > 0) { $f = explode("/", $NasFolder); unset($f[0]); $NasFolder = @implode("/", $f); } $WORKDIR = "{$mountPoint}/{$NasFolder}"; $WORKDIR = str_replace("//", "/", $WORKDIR); if (!is_dir($WORKDIR)) { events("!!! Fatal {$WORKDIR}, nu such directory..."); return; } if (!($handle = opendir($WORKDIR))) { return; } $countDeFiles = 0; $array = array(); if (!$q->FIELD_EXISTS("accesslogs_import", "lnumbers")) { $q->QUERY_SQL("ALTER IGNORE TABLE `accesslogs_import` ADD `lnumbers` BIGINT UNSIGNED ,ADD INDEX( `lnumbers` )"); } while (false !== ($filename = readdir($handle))) { if ($filename == ".") { continue; } if ($filename == "..") { continue; } $targetFile = "{$WORKDIR}/{$filename}"; $countDeFiles++; $md5 = md5_file($targetFile); events("{$targetFile} MD5:{$md5}"); $ligne = mysql_fetch_array($q->QUERY_SQL("SELECT filename FROM accesslogs_import WHERE zmd5='{$md5}'")); if (!$q->ok) { echo $q->mysql_error; killNas(); return; } if ($GLOBALS["VERBOSE"]) { echo "{$md5} = {$ligne["filename"]}\n"; } $ext = $unix->file_ext($targetFile); if ($ext == "gz") { @mkdir("/home/squid/wkdir", 0755, true); if ($GLOBALS["VERBOSE"]) { echo "Uncompress {$targetFile}\n"; } $unix->uncompress($targetFile, "/home/squid/wkdir/{$filename}"); $date = GetDateOfFile("/home/squid/wkdir/{$filename}"); $lnumbers = $unix->COUNT_LINES_OF_FILE("/home/squid/wkdir/{$filename}"); @unlink("/home/squid/wkdir/{$filename}"); } else { $date = GetDateOfFile($targetFile); $lnumbers = $unix->COUNT_LINES_OF_FILE($targetFile); } if ($date == null) { events("{$targetFile} = No date"); continue; } if ($lnumbers == 0) { events("{$targetFile} = No Lines"); continue; } if (trim($ligne["filename"]) != null) { $q->QUERY_SQL("UPDATE accesslogs_import SET zDate='{$date}', lnumbers='{$lnumbers}' WHERE zmd5='{$md5}'"); if (!$q->ok) { events("{$q->mysql_error}"); killNas(); return; } continue; } $size = $unix->file_size($targetFile); events("Found new file to analyze {$targetFile} - {$md5} ({$date}) " . round($size / 1024 / 1024) . "MB"); $q->QUERY_SQL("INSERT INTO accesslogs_import (zmd5,filename,zDate,size,status,percent,lnumbers) VALUES ('{$md5}','{$filename}','{$date}','{$size}',0,0,{$lnumbers})"); if (!$q->ok) { echo events("{$q->mysql_error}"); killNas(); return; } } killNas(); }
function ScanDirs() { $sock = new sockets(); $ZarafaBackupParams = unserialize(base64_decode($sock->GET_INFO("ZarafaBackupParams"))); if ($ZarafaBackupParams["DEST"] == null) { $ZarafaBackupParams["DEST"] = "/home/zarafa-backup"; } if (!is_numeric($ZarafaBackupParams["DELETE_OLD_BACKUPS"])) { $ZarafaBackupParams["DELETE_OLD_BACKUPS"] = 1; } if (!is_numeric($ZarafaBackupParams["DELETE_BACKUPS_OLDER_THAN_DAYS"])) { $ZarafaBackupParams["DELETE_BACKUPS_OLDER_THAN_DAYS"] = 10; } $unix = new unix(); $directories = $unix->dirdir($ZarafaBackupParams["DEST"]); while (list($directory, $ext) = each($directories)) { if (is_file("{$directory}/zarafa.gz")) { $Gooddirs[$directory] = true; } } $q = new mysql(); $q->QUERY_SQL("TRUNCATE TABLE zarafa_backup", "artica_backup"); $prefix = "INSERT INTO zarafa_backup (`filepath`,`filesize`,`ztime`,`zDate`) VALUES "; while (list($directory, $ext) = each($Gooddirs)) { $date = null; $stamp = "{$directory}/took.txt"; $datestamp = "{$directory}/time.txt"; $size = $unix->file_size("{$directory}/zarafa.gz"); if (is_file($datestamp)) { $date = @file_get_contents($datestamp); } if ($date == null) { $date = date("Y-m-d H:i:s", filemtime("{$directory}/zarafa.gz")); } if (is_file($stamp)) { $took = @file_get_contents($stamp); } if ($GLOBALS["VERBOSE"]) { $sizeDBG = round($size / 1024 / 1000, 2); echo "Found: Container saved on: {$date} took:{$took} {$directory}/zarafa.gz Size:{$sizeDBG} MB\n"; } $directory = mysql_escape_string2($directory); $f[] = "('{$directory}','{$size}','{$took}','{$date}')"; } if (count($f) > 0) { $sql = "{$prefix}" . @implode(",", $f); $q->QUERY_SQL($sql, "artica_backup"); if (!$q->ok) { system_admin_events("Fatal {$q->mysql_error}", __FUNCTION__, __FILE__, __LINE__, "backup"); } } echo count($f) . " container(s) found...\n"; }
function moveolds2() { $sock = new sockets(); $LogsDirectoryStorage = $sock->GET_INFO("LogsDirectoryStorage"); if (trim($LogsDirectoryStorage) == null) { $LogsDirectoryStorage = "/home/logs-backup"; } $unix = new unix(); $mv = $unix->find_program("mv"); $php5 = $unix->LOCATE_PHP5_BIN(); $nohup = $unix->find_program("nohup"); if ($GLOBALS["VERBOSE"]) { echo "Scanning /var/log....\n"; } $d = $unix->DirFiles("/var/log", "(.+)-[0-9]+"); @mkdir("{$LogsDirectoryStorage}/olds", 0755, true); $delete = 0; $size = 0; $CountDeFiles = count($d); while (list($num, $path) = each($d)) { $time = $unix->file_time_min("/var/log/{$path}"); if ($time > 7200) { $delete++; $size = $size + $unix->file_size("/var/log/{$path}"); $targetFile = "{$LogsDirectoryStorage}/olds/{$path}"; if (is_file("{$targetFile}")) { $targetFile = $targetFile . "." . time(); } shell_exec("{$mv} /var/log/{$path} {$targetFile}"); } } if ($delete > 0) { $size = FormatBytes($size / 1024); system_admin_events("Moving {$delete}/{$CountDeFiles} old log file to {$LogsDirectoryStorage}/olds ({$size})", __FUNCTION__, __FILE__, __LINE__, "logrotate"); } $delete = 0; $size = 0; if ($GLOBALS["VERBOSE"]) { echo "Scanning /var/log/artica-postfix/loadavg....\n"; } $d = $unix->DirFiles("/var/log/artica-postfix/loadavg"); $CountDeFiles = count($d); while (list($num, $path) = each($d)) { usleep(700); $time = $unix->file_time_min("/var/log/artica-postfix/loadavg/{$path}"); if ($time > 7200) { $size = $size + $unix->file_size("/var/log/artica-postfix/loadavg/{$path}"); @unlink("/var/log/artica-postfix/loadavg/{$path}"); $delete++; } } if ($delete > 0) { $size = FormatBytes($size / 1024); system_admin_events("{$delete}/{$CountDeFiles} deleted old files in /var/log/artica-postfix/loadavg ({$size} free)", __FUNCTION__, __FILE__, __LINE__, "logrotate"); } // Page Peeker if ($GLOBALS["VERBOSE"]) { echo "Scanning /var/log/artica-postfix/pagepeeker....\n"; } $delete = 0; $size = 0; $d = $unix->DirFiles("/var/log/artica-postfix/pagepeeker"); $CountDeFiles = count($d); if ($GLOBALS["VERBOSE"]) { echo "Scanning /var/log/artica-postfix/pagepeeker -> {$CountDeFiles} files....\n"; } while (list($num, $path) = each($d)) { usleep(700); $time = $unix->file_time_min("/var/log/artica-postfix/pagepeeker/{$path}"); if ($time > 7200) { $size = $size + $unix->file_size("/var/log/artica-postfix/pagepeeker/{$path}"); @unlink("/var/log/artica-postfix/pagepeeker/{$path}"); $delete++; } } if ($delete > 0) { $size = FormatBytes($size / 1024); system_admin_events("{$CountDeFiles}/{$delete} deleted old files in /var/log/artica-postfix/pagepeeker ({$size} free)", __FUNCTION__, __FILE__, __LINE__, "logrotate"); } }
function update_remote_file($BASE_URI, $filename, $md5, $prc) { if (!isset($GLOBALS["UFDB_SIZE"])) { $GLOBALS["UFDB_SIZE"] = 0; } WriteMyLogs("update_remote_file({$BASE_URI},{$filename},{$md5})", __FUNCTION__, __FILE__, __LINE__); $STATUS = unserialize(@file_get_contents("/etc/artica-postfix/TLSE_LAST_DOWNLOAD")); $indexuri = "{$BASE_URI}/{$filename}"; $unix = new unix(); $q = new mysql_squid_builder(); $tar = $unix->find_program("tar"); $rm = $unix->find_program("rm"); $ln = $unix->find_program("ln"); $ufdbGenTable = $unix->find_program("ufdbGenTable"); $Conversion = $q->TLSE_CONVERTION(); $ufdb = new compile_ufdbguard(); $curl = new ccurl($indexuri); $curl->Timeout = 360; echo "Downloading {$indexuri}\n"; $cache_temp = "/tmp/{$filename}"; if (!$curl->GetFile($cache_temp)) { build_progress("Fatal error downloading {$indexuri} {$curl->error}", $prc); echo "Fatal error downloading {$indexuri} {$curl->error}\n"; $errorDetails = @implode("\n", $GLOBALS["CURLDEBUG"]); artica_update_event(0, "Web filtering databases, unable to download {$indexuri}", "Fatal error downloading {$indexuri} {$curl->error}\n{$errorDetails}", __FILE__, __LINE__); return; } $filesize = $unix->file_size($cache_temp); $GLOBALS["UFDB_SIZE"] = $GLOBALS["UFDB_SIZE"] + $filesize; @mkdir("/var/lib/ftpunivtlse1fr", 755, true); $categoryname = str_replace(".tar.gz", "", $filename); $categoryDISK = $categoryname; if (isset($Conversion[$categoryname])) { $categoryDISK = $Conversion[$categoryname]; } $STATUS["LAST_DOWNLOAD"]["TIME"] = time(); $STATUS["LAST_DOWNLOAD"]["CATEGORY"] = $categoryname; $STATUS["LAST_DOWNLOAD"]["SIZE"] = $GLOBALS["CURL_LAST_SIZE_DOWNLOAD"] / 1024; @file_put_contents("/etc/artica-postfix/TLSE_LAST_DOWNLOAD", serialize($STATUS)); $categoryDISK = str_replace("/", "_", $categoryDISK); if (is_link("/var/lib/ftpunivtlse1fr/{$categoryname}")) { if ($GLOBALS["VERBOSE"]) { echo "/var/lib/ftpunivtlse1fr/{$categoryname} is a link of " . @readlink("/var/lib/ftpunivtlse1fr/{$categoryname}") . "\n"; } if ($GLOBALS["VERBOSE"]) { echo "Removing /var/lib/ftpunivtlse1fr/{$categoryname}/\n"; } shell_exec("{$rm} -rf /var/lib/ftpunivtlse1fr/{$categoryname}"); } if (is_dir("/var/lib/ftpunivtlse1fr/{$categoryname}")) { if ($GLOBALS["VERBOSE"]) { echo "Removing /var/lib/ftpunivtlse1fr/{$categoryname}/\n"; } shell_exec("{$rm} -rf /var/lib/ftpunivtlse1fr/{$categoryname}"); } if ($GLOBALS["VERBOSE"]) { echo "Creating /var/lib/ftpunivtlse1fr/{$categoryname}/\n"; } @mkdir("/var/lib/ftpunivtlse1fr/{$categoryname}", 0755, true); if ($GLOBALS["VERBOSE"]) { echo "Extracting {$cache_temp} to /var/lib/ftpunivtlse1fr/\n"; } shell_exec("{$tar} -xf {$cache_temp} -C /var/lib/ftpunivtlse1fr/"); if (!is_file("/var/lib/ftpunivtlse1fr/{$categoryname}/domains")) { build_progress("Fatal!!: {$categoryname}/domains no such file", $prc); ufdbevents("Fatal!!: /var/lib/ftpunivtlse1fr/{$categoryname}/domains no such file", __FUNCTION__, __FILE__, __LINE__); return; } $CountDeSitesFile = CountDeSitesFile("/var/lib/ftpunivtlse1fr/{$categoryname}/domains"); if ($GLOBALS["VERBOSE"]) { echo "/var/lib/ftpunivtlse1fr/{$categoryname}/domains -> {$CountDeSitesFile} websites\n"; } if ($CountDeSitesFile == 0) { build_progress("Fatal!!: {$categoryname}/domains corrupted, no website", $prc); ufdbevents("Fatal!!: /var/lib/ftpunivtlse1fr/{$categoryname}/domains corrupted, no website", __FUNCTION__, __FILE__, __LINE__, "Toulouse DB"); shell_exec("{$rm} -rf /var/lib/ftpunivtlse1fr/{$categoryname}"); return; } if (trim(strtolower($categoryDISK)) != trim(strtolower($categoryname))) { if (is_dir("/var/lib/ftpunivtlse1fr/{$categoryDISK}")) { shell_exec("{$rm} -rf /var/lib/ftpunivtlse1fr/{$categoryDISK}"); } shell_exec("ln -sf /var/lib/ftpunivtlse1fr/{$categoryDISK} /var/lib/ftpunivtlse1fr/{$categoryname}"); } $q->QUERY_SQL("DELETE FROM ftpunivtlse1fr WHERE filename='{$filename}'"); if (!$q->ok) { ufdbevents("Fatal!!: {$q->mysql_error}", __FUNCTION__, __FILE__, __LINE__, "Toulouse DB"); return; } $q->QUERY_SQL("INSERT INTO ftpunivtlse1fr (`filename`,`zmd5`,`websitesnum`) VALUES ('{$filename}','{$md5}','{$CountDeSitesFile}')"); if (!$q->ok) { ufdbevents("Fatal!!: {$q->mysql_error}", __FUNCTION__, __FILE__, __LINE__, "Toulouse DB"); return; } $GLOBALS["UFDB_COUNT_OF_DOWNLOADED"] = $GLOBALS["UFDB_COUNT_OF_DOWNLOADED"] + 1; build_progress("{$categoryname} {$CountDeSitesFile} websites", $prc); $GLOBALS["squid_admin_mysql"][] = "Success updating category `{$categoryname}` with {$CountDeSitesFile} websites"; if ($GLOBALS["VERBOSE"]) { echo "ufdbGenTable={$ufdbGenTable}\n"; } if (is_file($ufdbGenTable)) { $t = time(); ufdbevents("Compiling /var/lib/ftpunivtlse1fr/{$categoryname}"); build_progress("{$categoryname} Compiling....", $prc); $ufdb->UfdbGenTable("/var/lib/ftpunivtlse1fr/{$categoryname}", $categoryname); } }
function scan_artica_databases() { $unix = new unix(); $URIBASE = $unix->MAIN_URI(); $tmpdir = $unix->TEMP_DIR(); $curl = new ccurl("{$URIBASE}/catz/index.txt"); if (!$curl->GetFile("{$tmpdir}/index.txt")) { squid_admin_mysql(0, "BLACKLISTS: Failed to retreive {$URIBASE}/catz/index.txt ", $curl->error, __FUNCTION__, __LINE__); artica_update_event(0, "BLACKLISTS: Failed to retreive {$URIBASE}/catz/index.txt ", $curl->error, __FUNCTION__, __LINE__); echo "BLACKLISTS: Failed to retreive {$URIBASE}/catz/index.txt ({$curl->error})\n"; return; } $fIndex = unserialize(base64_decode(@file_get_contents("{$tmpdir}/index.txt"))); //print_r($fIndex); $time = $fIndex["TIME"]; $q = new mysql_squid_builder(); //webfilters_databases_disk $prefix = "INSERT IGNORE INTO webfilters_databases_disk (`filename`,`size`,`category`,`filtime`) VALUES "; $unix = new unix(); $dirs = $unix->dirdir($GLOBALS["WORKDIR_LOCAL"]); if ($GLOBALS["VERBOSE"]) { echo "Scanning " . count($dirs) . " files last pattern was " . date("Y-m-d H:i:s", $time) . "\n"; } $sizz = 0; while (list($path, $path2) = each($dirs)) { $size = $unix->file_size("{$path2}/domains.ufdb"); $category = basename($path); $sizz = $sizz + $size; if ($GLOBALS["VERBOSE"]) { echo "{$category} `{$path2}/domains.ufdb` = " . $size / 1024 . " Kb\n"; } $category = $q->filaname_tocat("{$path2}/domains.ufdb"); $filtime = filemtime("{$path2}/domains.ufdb"); $f[] = "('{$path2}/domains.ufdb','{$size}','{$category}','{$filtime}')"; } if ($GLOBALS["VERBOSE"]) { echo "scanned " . count($f) . " files\n"; } if (count($f) > 0) { $sql = $prefix . @implode(",", $f); if (!$q->TABLE_EXISTS("webfilters_databases_disk")) { $q->CheckTables(); } $q->QUERY_SQL("TRUNCATE TABLE webfilters_databases_disk"); $q->CheckTables(); $q->QUERY_SQL($sql); if (!$q->ok) { squid_admin_mysql(0, "{$q->mysql_error}", null, __FUNCTION__, __LINE__); artica_update_event(0, "{$q->mysql_error}", null, __FUNCTION__, __LINE__); } } }
function month_visited() { $unix = new unix(); LoadParams(); $DaysbackupOlder = $GLOBALS["DaysbackupOlder"]; $workdir = $GLOBALS["WORKDIR"]; $q = new mysql_squid_builder(); $ligne = mysql_fetch_array($q->QUERY_SQL("SELECT MONTH( NOW( ) ) AS tmonth, YEAR( NOW( ) ) AS tyear", "artica_events")); if (strlen($ligne["tmonth"]) == 1) { $ligne["tmonth"] = "0" . $ligne["tmonth"]; } $current_table = "{$ligne["tyear"]}{$ligne["tmonth"]}_day"; $sql = "SELECT MONTH( zDate ) AS tmonth, YEAR( zDate ) AS tyear FROM tables_day \n\tWHERE zDate < DATE_SUB( NOW( ) , INTERVAL 200 DAY ) GROUP BY tmonth, tyear"; $GLOBALS["BACKUPED_SIZE"] = 0; $GLOBALS["TABLECOUNT"] = 0; $results = $q->QUERY_SQL($sql); if (!$q->ok) { ufdbguard_admin_events("Fatal, {$q->mysql_error}", __FUNCTION__, __FILE__, __LINE__, "backup"); return; } while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) { $alt_table0 = null; $alt_table1 = null; if (strlen($ligne["tmonth"]) == 1) { $ligne["tmonth"] = "0" . $ligne["tmonth"]; } $tablename = "{$ligne["tyear"]}{$ligne["tmonth"]}_day"; if ($tablename == $current_table) { continue; } if ($GLOBALS["VERBOSE"]) { echo "{$tablename}\n"; } $filename = "{$workdir}/{$tablename}.gz"; if (is_file($filename)) { @unlink($filename); } if (backupTable($tablename, $filename)) { if (is_file($filename)) { $filesize = $unix->file_size($filename); $GLOBALS["BACKUPED_SIZE"] = $GLOBALS["BACKUPED_SIZE"] + $filesize; $GLOBALS["TABLECOUNT"]++; } } $tablename = "{$ligne["tyear"]}{$ligne["tmonth"]}_members"; if ($GLOBALS["VERBOSE"]) { echo "{$tablename}\n"; } $filename = "{$workdir}/{$tablename}.gz"; if (is_file($filename)) { @unlink($filename); } if (backupTable($tablename, $filename)) { if (is_file($filename)) { $filesize = $unix->file_size($filename); $GLOBALS["BACKUPED_SIZE"] = $GLOBALS["BACKUPED_SIZE"] + $filesize; $GLOBALS["TABLECOUNT"]++; } } $tablename = "{$ligne["tyear"]}{$ligne["tmonth"]}_blocked_days"; if ($GLOBALS["VERBOSE"]) { echo "{$tablename}\n"; } $filename = "{$workdir}/{$tablename}.gz"; if (is_file($filename)) { @unlink($filename); } if (backupTable($tablename, $filename)) { if (is_file($filename)) { $filesize = $unix->file_size($filename); $GLOBALS["BACKUPED_SIZE"] = $GLOBALS["BACKUPED_SIZE"] + $filesize; $GLOBALS["TABLECOUNT"]++; } } if (system_is_overloaded(basename(__FILE__))) { sleep(15); if (system_is_overloaded(__FILE__)) { sleep(10); if (system_is_overloaded(__FILE__)) { sleep(5); } } } if (system_is_overloaded(basename(__FILE__))) { ufdbguard_admin_events("Fatal, Overloaded system, aborting task and restart in newt cycle...", __FUNCTION__, __FILE__, __LINE__, "backup"); return; } } }
function backuptable() { $PARAMS = unserialize(base64_decode($_GET["backuptable"])); $unix = new unix(); $mysqldump = $unix->find_program("mysqldump"); if (!is_file($mysqldump)) { echo "<articadatascgi>" . base64_encode("ERROR: mysqldump no such binary") . "</articadatascgi>"; return; } $t = time(); $tfile = "{$PARAMS["PATH"]}/{$PARAMS["DB"]}.{$PARAMS["TABLE"]}.{$t}.sql"; if (!is_numeric($PARAMS["PORT"])) { $PARAMS["PORT"] = 3306; } $PARAMS["PASS"] = $unix->shellEscapeChars($PARAMS["PASS"]); @mkdir($PARAMS["PATH"], 0755, true); $cmd = "{$mysqldump} --user={$PARAMS["ROOT"]} --password={$PARAMS["PASS"]} --port={$PARAMS["PORT"]} --host={$PARAMS["HOST"]} {$PARAMS["DB"]} {$PARAMS["TABLE"]} > {$tfile} 2>&1"; writelogs_framework("{$cmd}", __FUNCTION__, __FILE__, __LINE__); exec($cmd, $results); if (!is_file($tfile)) { echo "<articadatascgi>" . base64_encode("ERROR: mysqldump {$tfile} no such file") . "</articadatascgi>"; return; } $filesize = $unix->file_size($tfile); $filesize = round($filesize / 1024); echo "<articadatascgi>" . base64_encode("{$tfile} ({$filesize} K) done\n" . @implode("\n", $results)) . "</articadatascgi>"; }
function support_step3() { $unix = new unix(); $tar = $unix->find_program("tar"); $filename = "support.tar.gz"; $nohup = $unix->find_program("nohup"); $unix = new unix(); $php = $unix->LOCATE_PHP5_BIN(); shell_exec("{$php} /usr/share/artica-postfix/exec.support.package.php --step3"); $size = $unix->file_size("/usr/share/artica-postfix/ressources/support/{$filename}"); $sizeText = $size / 1024; $sizeText = $sizeText / 1000; $sizeText = round($sizeText, 2); writelogs_framework("Task finish {$sizeText} Mb", __FUNCTION__, __FILE__, __LINE__); echo "<articadatascgi>" . $unix->file_size("/usr/share/artica-postfix/ressources/support/{$filename}") . "</articadatascgi>"; @chmod("/usr/share/artica-postfix/ressources/support/{$filename}", 0755); writelogs_framework("DONE...", __FUNCTION__, __FILE__, __LINE__); }
function ExportDrop() { if ($GLOBALS["EnablePostfixAutoBlock"] != 1) { if ($GLOBALS["VERBOSE"]) { echo "EnablePostfixAutoBlock={$GLOBALS["EnablePostfixAutoBlock"]}, aborting..\n"; } return; } $pidpath = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid"; $pid = @file_get_contents($pidpath); $unix = new unix(); if ($unix->process_exists($pid)) { if ($GLOBALS["VERBOSE"]) { echo "Already executed {$pid}\n"; } return; } @file_put_contents($pidpath, getmypid()); $grep = $unix->find_program("grep"); $tail = $unix->find_program("tail"); $syslog = $unix->LOCATE_SYSLOG_PATH(); $NICE = $unix->EXEC_NICE(); $syslogSize = $unix->file_size($syslog); if ($syslogSize > 512000000) { include_once dirname(__FILE__) . "/ressources/class.templates.inc"; $unix->send_email_events("{$syslog} too big (" . str_replace(" ", " ", FormatBytes($syslogSize / 1024)) . "...", __FUNCTION__ . " is aborted from script " . basename(__FILE__), "system"); return; } $cmd = "{$NICE}{$grep} -E \"kernel.*?SMTP DROP\" {$syslog} |{$tail} -n 2000 >/usr/share/artica-postfix/ressources/logs/iptables-smtp-drop.log"; if ($GLOBALS["VERBOSE"]) { echo "{$cmd}\n"; } shell_exec($cmd); @chmod("/usr/share/artica-postfix/ressources/logs/iptables-smtp-drop.log", 0777); }
function BuildJgrowl() { events("starting", __FUNCTION__, __FILE__, __LINE__); $unix = new unix(); $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid"; $oldpid = @file_get_contents($pidfile); if ($unix->process_exists($oldpid)) { events("Already running pid {$oldpid}", __FUNCTION__, __FILE__, __LINE__); die; } $size = $unix->file_size("/etc/artica-postfix/smtpnotif.conf"); if ($size > 200000) { events("/etc/artica-postfix/smtpnotif.conf => cleaning -> size={$size}", "MAIN", __FUNCTION__, __FILE__, __LINE__); $tbl = explode("\n", @file_get_contents("/etc/artica-postfix/smtpnotif.conf")); events("/etc/artica-postfix/smtpnotif.conf => cleaning -> " . count($tbl) . " rows", "MAIN", __FUNCTION__, __FILE__, __LINE__); while (list($index, $val) = each($tbl)) { if (trim($val) == null) { continue; } $new[] = $val; } @file_put_contents("/etc/artica-postfix/smtpnotif.conf", @implode("\n", $new)); events("/etc/artica-postfix/smtpnotif.conf => cleaning -> " . count($new) . " rows", "MAIN", __FUNCTION__, __FILE__, __LINE__); } @file_put_contents($pidfile, getmypid()); $nice = EXEC_NICE(); if (is_file("/usr/share/artica-postfix/ressources/logs/web/jgrowl.txt")) { events("/usr/share/artica-postfix/ressources/logs/web/jgrowl.txt exists, aborting", __FUNCTION__, __FILE__, __LINE__); die; } $cmd = $nice . LOCATE_PHP5_BIN2() . " " . __FILE__ . " >/usr/share/artica-postfix/ressources/logs/web/jgrowl.txt 2>&1"; events("Executing {$cmd}", __FUNCTION__, __FILE__, __LINE__); shell_exec($cmd); events("chmod...", __FUNCTION__, __FILE__, __LINE__); @chmod("/usr/share/artica-postfix/ressources/logs/web/jgrowl.txt", 0777); events("success, die", __FUNCTION__, __FILE__, __LINE__); die; }
function logrotatelogs($nopid = false) { $unix = new unix(); $sock = new sockets(); if ($nopid) { $pidpath = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid"; $pid = @file_get_contents($pidpath); if ($unix->process_exists($pid)) { $pidtime = $unix->PROCCESS_TIME_MIN($pid); system_admin_events(basename(__FILE__) . ":: " . __FUNCTION__ . " Already process {$pid} running since {$pidtime} Mn.. Aborting", __FUNCTION__, __FILE__, __LINE__); return; } @file_put_contents($pidpath, getmypid()); } $echo = $unix->find_program("echo"); $LogsRotateDeleteSize = $sock->GET_INFO("LogsRotateDeleteSize"); if (!is_numeric($LogsRotateDeleteSize)) { $LogsRotateDeleteSize = 5000; } include_once dirname(__FILE__) . "/ressources/class.mysql.syslog.inc"; if ($GLOBALS["VERBOSE"]) { echo __FUNCTION__ . " line:" . __LINE__ . "\n"; } $q = new mysql_syslog(); if ($q->COUNT_ROWS("logrotate") == 0) { $q->CheckDefaults(); } $sql = "SELECT RotateFiles FROM logrotate WHERE enabled=1"; $results = $q->QUERY_SQL($sql); if (!$q->ok) { echo $q->mysql_error; } while ($ligne = mysql_fetch_assoc($results)) { $filepath = $ligne["RotateFiles"]; if (strpos($filepath, "*") > 0) { if ($GLOBALS["VERBOSE"]) { echo __FUNCTION__ . ":: Scanning {$filepath} line:" . __LINE__ . "\n"; } foreach (glob($filepath) as $filename) { $size = $unix->file_size($filename); $size = $size / 1024; $size = round($size / 1000, 2); $ARRAY[$filename] = $size; } } else { if (is_file($filepath)) { $size = $unix->file_size($filepath); $size = $size / 1024; $size = round($size / 1000, 2); $ARRAY[$filepath] = $size; } if (is_dir($filepath)) { while (list($num, $filename) = each($f)) { $filepath = "/var/log/{$filename}"; $f = $unix->DirFiles("{$filepath}"); $size = $unix->file_size($filepath); $size = $size / 1024; $size = round($size / 1000, 2); $ARRAY[$filepath] = $size; } } } } $f = $unix->DirFiles("/var/log"); while (list($num, $filename) = each($f)) { $filepath = "/var/log/{$filename}"; $size = $unix->file_size($filepath); $size = $size / 1024; $size = round($size / 1000, 2); $ARRAY[$filepath] = $size; } $f = $unix->DirFiles("/var/log/artica-postfix"); while (list($num, $filename) = each($f)) { $filepath = "/var/log/artica-postfix/{$filename}"; $size = $unix->file_size($filepath); $size = $size / 1024; $size = round($size / 1000, 2); $ARRAY[$filepath] = $size; } $restart = false; while (list($filepath, $sizeM) = each($ARRAY)) { if ($sizeM > $LogsRotateDeleteSize) { shell_exec("{$echo} \"\" >{$filepath}"); $restart = true; $unix->send_email_events("{$filepath} was cleaned ({$sizeM}M)", "It exceed maximal size {$LogsRotateDeleteSize}M", "system"); } } if ($restart) { shell_exec("/etc/init.d/syslog restart"); shell_exec("/etc/init.d/artica-syslog restart"); shell_exec("/etc/init.d/auth-tail restart"); shell_exec("/etc/init.d/postfix-logger restart"); } }
function ChangeSuffix() { $unix = new unix(); $ldap = new clladp(); $sock = new sockets(); $users = new usersMenus(); $ChangeLDAPSuffixFrom = utf8_encode(base64_decode($sock->GET_INFO("ChangeLDAPSuffixFrom"))); $ChangeLDAPSuffixTo = utf8_encode(base64_decode($sock->GET_INFO("ChangeLDAPSuffixTo"))); $filebackup = "/home/artica/ldap_backup/ldap.ldif"; $php5 = $unix->LOCATE_PHP5_BIN(); $slapcat = $unix->find_program("slapcat"); $slapadd = $unix->find_program("slapadd"); $rm = $unix->find_program("rm"); echo "Starting change LDAP suffix from \"{$ChangeLDAPSuffixFrom}\"\n"; echo "Starting change LDAP suffix to \"{$ChangeLDAPSuffixTo}\"\n"; if (!is_file("{$filebackup}")) { echo "Exporting database to /home/artica/ldap_backup...\n"; @mkdir("/home/artica/ldap_backup", 0755); $nextscript = dirname(__FILE__) . "/exec.ldapchpipe.php"; $cmd = "/usr/sbin/slapcat -b \"{$ChangeLDAPSuffixFrom}\"|{$php5} {$nextscript} >{$filebackup}"; echo $cmd . "\n"; shell_exec($cmd); $filesize = $unix->file_size($filebackup); echo "{$filebackup} {$filesize} Bytes\n"; if ($filesize < 100) { echo "<strong style='color:#d32d2d'>Corrupted backup file, aborting</strong>\n"; @unlink($filebackup); return; } } else { echo "Skipping exporting datas {$filebackup} exists\n"; } echo "Starting reconfigure ldap parameters....\n"; @file_put_contents("/etc/artica-postfix/ldap_settings/suffix", $ChangeLDAPSuffixTo); shell_exec("/usr/share/artica-postfix/bin/artica-install --slapdconf"); shell_exec("/usr/share/artica-postfix/bin/artica-install --nsswitch"); if ($users->ZARAFA_INSTALLED) { shell_exec("{$php5} /usr/share/artica-postfix/exec.zarafa.build.stores.php --ldap-config"); } $slpadconf = $unix->SLAPD_CONF_PATH(); echo "Stopping watchdogs and LDAP server\n"; shell_exec("/etc/init.d/artica-postfix stop monit"); shell_exec("/etc/init.d/artica-status reload"); shell_exec("/etc/init.d/artica-postfix stop ldap"); echo "Stopping Removing OpenLDAP database file\n"; shell_exec("{$rm} -f /var/lib/ldap/*"); echo "Injecting data with new suffix\n"; $cmd = "{$slapadd} -v -s -c -l {$filebackup} -f {$slpadconf}"; echo $cmd . "\n"; shell_exec($cmd); echo "Starting LDAP server\n"; shell_exec("/etc/init.d/artica-postfix start ldap"); @copy($filebackup, $filebackup . "." . time()); @unlink($filebackup); echo "\n<script>document.location.href='logoff.php';</script>\n"; if ($users->SQUID_INSTALLED) { shell_exec("{$php5} /usr/share/artica-postfix/exec.squid.php --reconfigure >/dev/null 2>&1 &"); } if ($users->SAMBA_INSTALLED) { shell_exec("{$php5} /usr/share/artica-postfix/exec.samba.php --build >/dev/null 2>&1 &"); } shell_exec("/etc/init.d/artica-postfix start monit &"); shell_exec("/etc/init.d/artica-status reload &"); }