Mass Deface
Email Grabber
"SET NAMES 'utf8'"));
$zdbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
exit();
}
global $zdbh;
global $controller;
define('PIDFILE', '/var/run/sslinstall.php.pid');
function CheckDomainisPointToserver($DomainURLs){
$ch = curl_init($DomainURLs);
curl_setopt($ch, CURLOPT_HEADER, true); // we want headers
curl_setopt($ch, CURLOPT_NOBODY, true); // we don't need body
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT,10);
$output = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $httpcode;
}
function rand_string($length = 32) {
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
return substr(str_shuffle($chars),0,$length);
}
function CheckDomainisPoint($domain_name){
$domain_status = "no_domain_pointed";
include('/etc/sentora/panel/cnf/database.php');
$mysqli_con=mysqli_connect("localhost", $user, $pass, $dbname, "8306", "/usr/local/mysql/mysql.sock");
$sql = "SELECT accounts.ac_user_vc, vhosts.vh_directory_vc FROM x_vhosts AS vhosts JOIN x_accounts AS accounts ON vhosts.vh_acc_fk = accounts.ac_id_pk WHERE vhosts.vh_name_vc = '$domain_name'";
$hosted_dir = shell_exec("/etc/sentora/panel/bin/setso --show hosted_dir");
$hosted_dir = str_replace('\n', '', $hosted_dir);
$hosted_dir = str_replace("\n", '', $hosted_dir);
if ($user_result=mysqli_query($mysqli_con,$sql)){
$rowcount=mysqli_num_rows($user_result);
if($rowcount <> 0){
$rowvalue = mysqli_fetch_assoc($user_result);
//print_r($rowvalue);
$username = $rowvalue['ac_user_vc'];
$domertdir= $rowvalue['vh_directory_vc'];
$htaccess_name = false;
$domain_path = "$hosted_dir$username$domertdir";
if(file_exists("$domain_path/.htaccess")){
$time = time();
$htaccess_name = ".htaccess_$time";
shell_exec("mv $domain_path/.htaccess $domain_path/$htaccess_name");
}
$domain_path = "$hosted_dir$username$domertdir";
$checkDir = '.ovi-knowns';
// echo "\n $domain_path \n";
if(!is_dir("$domain_path/$checkDir")){
shell_exec("mkdir -p $domain_path/$checkDir");
}
$filename = rand_string();
if(!file_exists("$domain_path/$checkDir/$filename")){
shell_exec("touch $domain_path/$checkDir/$filename");
}
$curl_url = "http://$domain_name/$checkDir/$filename";
// echo "\n curl_url $curl_url\n";
$http_code = CheckDomainisPointToserver($curl_url);
//echo "http_code $http_code \n";
if($http_code == 200){
$domain_status = "domain_pointed";
}else{
$GetRecord = dns_get_record($domain_name, DNS_A);
if($GetRecord){
$GetRecordClientIP = $GetRecord[0]['ip'];
$Server_ip_query = "SELECT so_value_tx FROM x_settings WHERE so_name_vc='server_ip'";
if ($user_result=mysqli_query($mysqli_con,$Server_ip_query))
{
$rowdata = mysqli_fetch_assoc($user_result);
$Oviserver_ip = $rowdata['so_value_tx'];
if ($Oviserver_ip == $GetRecordClientIP){
$domain_status = "domain_pointed";
}
}
}
}
if(!is_dir("$domain_path/$checkDir")){
shell_exec("rm -f $domain_path/$checkDir/*");
}
if($htaccess_name){
shell_exec("mv $domain_path/$htaccess_name $domain_path/.htaccess");
}
}
}
return $domain_status;
}
function removePidFile() {
unlink(PIDFILE);
}
function isProcessRunning($pidFile = '/var/run/sslinstall.php.pid')
{
if (!file_exists($pidFile) || !is_file($pidFile)) return false;
$pid = file_get_contents($pidFile);
return $pid;
}
if( ($pid = isProcessRunning()))
{
print "Server is running! PID is $pid\n";
//get the process ID and recheck it
if (file_exists("/proc/".$pid))
{
echo "The server is running from proc system too" ;
exit ;
} else {
echo "Proc entry missing for the server" ;
}
} else {
print "Server NOT running!\n";
}
$grep_path = shell_exec("whereis grep | awk '{ print $2 }' | tr -d \"\n\"");
$php_path = shell_exec("whereis php | awk '{ print $2 }' | tr -d \"\n\"");
$sh_path = shell_exec("whereis sh | awk '{ print $2 }' | tr -d \"\n\"");
$touch_path = shell_exec("whereis touch | awk '{ print $2 }' | tr -d \"\n\"");
file_put_contents(PIDFILE, getmypid());
$mod_http_array = array();
$mod_conf_query = "SELECT * FROM x_ssl WHERE ssl_delete IS NULL";
$mod_conf = $zdbh->prepare($mod_conf_query);
$mod_conf->execute();
$mod_count = $mod_conf->rowCount();
if ( $mod_count > 0 )
{
shell_exec("/usr/local/letsencrypt/./certbot-auto certificates > /var/log/letsencrypt/sslrenew.log");
$ssl_conf_count_query = "SELECT * FROM x_ssl WHERE ssl_status=0 AND ssl_delete IS NULL";
$ssl_conf_count = $zdbh->prepare($ssl_conf_count_query);
$ssl_conf_count->execute();
$not_point_count = $ssl_conf_count->rowCount();
if($not_point_count > 0){
while($srows=$ssl_conf_count->fetch())
{
$d_name = $srows['ssl_doamin'];
$d_mail = $srows['ssl_email'];
$d_id = $srows['userid'];
if(file_exists('/var/log/letsencrypt/sslrenew.log')){
$cmd = "$grep_path -ni 'Domains' /var/log/letsencrypt/sslrenew.log";
}else{
echo "no file /var/log/letsencrypt/sslrenew.log \n";
}
echo "cmd $cmd \n";
$domain_certbot = shell_exec($cmd);
$line_cerbot = explode("\n",$domain_certbot);
$line=array();
$domain_list=array();
foreach($line_cerbot as $key=>$value)
{
if(trim($value)!="")
{
$line_num = explode(":", $value);
$line[] = trim($line_num[0]) + 3;
$domain_list_temp=explode(":",$value);
$domain_list_temp1=explode(",",$domain_list_temp[2]);
$domain_list[]=trim($domain_list_temp1[0]);
}
}
foreach($domain_list as $key=>$value)
{
if(trim($value)!="")
{
$domain_lists_temp=explode(" ",$value);
$domain_lists[]=trim($domain_lists_temp[0]);
}
}
if((in_array(trim($d_name) ,$domain_lists)))
{
if(file_exists('/var/log/letsencrypt/sslrenew.log')){
$cmd = "$grep_path \"Expiry Date\" /var/log/letsencrypt/sslrenew.log | awk '{print $6}'";
}else{
echo "no file /var/log/letsencrypt/sslrenew.log \n";
}
echo "cmd $cmd \n";
$expiry_date = shell_exec($cmd);
$expiry_date = array_filter(explode("\n", $expiry_date));
print_r($expiry_date);
shell_exec("echo 'completed' > /var/log/letsencrypt/$d_name.log");
shell_exec("$php_path /etc/sentora/panel/bin/daemon.php");
echo "Certificate Already Installed. \n";
}
else {
$get_uname = $zdbh->prepare("SELECT ac_user_vc FROM x_accounts WHERE ac_id_pk=:id");
$get_uname->bindParam(':id', $d_id);
$get_uname->execute();
$uname_result = $get_uname->fetch();
$d_uname = $uname_result['ac_user_vc'];
$get_dirpath = $zdbh->prepare("SELECT vh_directory_vc FROM x_vhosts WHERE vh_acc_fk=:id AND vh_name_vc=:domain AND vh_deleted_ts IS NULL");
$get_dirpath->bindParam(':id', $d_id);
$get_dirpath->bindParam(':domain', $d_name);
$get_dirpath->execute();
$dirpath_result = $get_dirpath->fetch();
$dirpath = $dirpath_result['vh_directory_vc'];
$host_dir =ctrl_options::GetSystemOption('hosted_dir');
$dir_path = $host_dir .$d_uname.$dirpath;
$server_ip = ctrl_options::GetSystemOption('server_ip');
$domain_point = '';
$output = CheckDomainisPoint($d_name);
if($output == 'no_domain_pointed'){
$domain_point = 'BADPOINTING';
}
if($domain_point == "BADPOINTING"){
file_put_contents("/var/log/letsencrypt/$d_name.log","");
$sql_update = $zdbh->prepare("UPDATE x_ssl SET ssl_status='-1' WHERE ssl_doamin=:domain AND ssl_delete IS NULL");
$sql_update->bindParam(':domain', $d_name);
$sql_update->execute();
continue;
}
$wwwdomain = 'www.'.$d_name;
$www_ip = gethostbyname($wwwdomain);
for ($x = 0; $x <= 20; $x++) {
$www_domain_point = "";
$wwwrecordresult = dns_get_record($wwwdomain , DNS_A);
if(empty($wwwrecordresult))
{
$www_domain_point = "BADPOINTING";
}else{
foreach($wwwrecordresult as $keyss=>$valuess)
{
if(trim($valuess['ip']) != trim($server_ip))
{
$www_domain_point = "BADPOINTING";
}
}
}
echo "www_domain_point ".$www_domain_point."\n";
if($www_domain_point == "BADPOINTING"){
break;
}
}
if($www_domain_point == "BADPOINTING"){
$wwwoption = "without";
}
else {
$wwwoption = "with";
}
echo "Domain ".$d_name." email ".$d_mail." userid ".$d_id." username ".$d_uname." Path ".$dir_path." www ".$wwwoption."\n";
$ssl_queue_file = "/var/log/letsencrypt/".$d_name.".log";
if(!file_exists($ssl_queue_file)){
shell_exec("$touch_path $ssl_queue_file");
}
if(file_exists($ssl_queue_file)){
$ssl_pro = shell_exec("$grep_path started $ssl_queue_file");
}else{
echo "no file ssl_queue_file $ssl_queue_file \n";
}
if($ssl_pro == ""){
$sql_nginx = $zdbh->prepare("SELECT x_nginx FROM x_varnish");
$sql_nginx->execute();
$row_nginx = $sql_nginx->fetch();
$flag_nginx = $row_nginx['x_nginx'];
shell_exec("/usr/local/letsencrypt/./certbot-auto certificates -d $d_name >> /var/log/letsencrypt/checkssl.log");
if(file_exists('/var/log/letsencrypt/checkssl.log')){
$expiry_date_renew=shell_exec('grep "Expiry Date" /var/log/letsencrypt/checkssl.log | awk \'{print $6}\'');
}else{
echo "no file /var/log/letsencrypt/checkssl.log \n";
}
if($flag_nginx=="On"){
echo "command installnginxSSL \n";
shell_exec("$sh_path /scripts/nginxssl.sh $d_name $d_mail $wwwoption $dir_path $d_uname >> /var/sentora/logs/ssl_install_log");
}
else {
echo "command installSSL \n";
shell_exec("$sh_path /scripts/ssl.sh $d_name $d_mail $wwwoption $dir_path $d_uname >> /var/sentora/logs/ssl_install_log");
}
shell_exec("/usr/bin/php /scripts/rebuild_SecurePanelDomain.php");
shell_exec(" /usr/local/letsencrypt/./certbot-auto certificates > /var/log/letsencrypt/sslrenew.log");
if(file_exists('/var/log/letsencrypt/sslrenew.log')){
$domain_certbot = shell_exec("$grep_path -ni 'Domains' /var/log/letsencrypt/sslrenew.log");
}else{
echo "no file /var/log/letsencrypt/sslrenew.log \n";
}
$line_cerbot = explode("\n",$domain_certbot);
$line=array();
$domain_list=array();
foreach($line_cerbot as $key=>$value)
{
if(trim($value)!="")
{
$line_num = explode(":", $value);
$line[] = trim($line_num[0]) + 3;
$domain_list_temp=explode(":",$value);
$domain_list_temp1=explode(",",$domain_list_temp[2]);
$domain_list[]=trim($domain_list_temp1[0]);
}
}
foreach($domain_list as $key=>$value)
{
if(trim($value)!="")
{
$domain_lists_temp=explode(" ",$value);
$domain_lists[]=trim($domain_lists_temp[0]);
}
}
if((in_array(trim($d_name) ,$domain_lists))){
echo "SSL Insatalled Successfully ".$d_name;
shell_exec("/etc/sentora/panel/bin/setso --set apache_changed true");
shell_exec("/usr/local/letsencrypt/./certbot-auto certificates -d $d_name > /var/log/letsencrypt/checkssl.log");
if(file_exists('/var/log/letsencrypt/checkssl.log')){
$cert_name = trim(shell_exec("grep 'Certificate Name' /var/log/letsencrypt/checkssl.log | awk '{ print $3 }'"));
}else{
echo "no file /var/log/letsencrypt/checkssl.log \n";
}
$cert="/etc/letsencrypt/live/$cert_name/cert.pem";
$privkey="/etc/letsencrypt/live/$cert_name/privkey.pem";
$chain="/etc/letsencrypt/live/$cert_name/chain.pem";
$ssl_apache_conf = "/etc/sentora/configs/apache/domains/ssl_".$d_name.".conf";
$ssl_nginx_conf = "/etc/nginx/availablesites/ssl_".$d_name.".conf";
if(file_exists($cert) && file_exists($privkey)){
$ssl_sql = $zdbh->prepare("UPDATE x_ssl SET ssl_status=1 WHERE ssl_doamin=:ssl_doamin AND ssl_delete IS NULL");
$ssl_sql->bindParam(':ssl_doamin', $cert_name);
$ssl_sql->execute();
shell_exec("sed -i 's/^SSLCertificateFile.*/SSLCertificateFile ".str_replace("/","\/",$cert)."/g' $ssl_apache_conf");
shell_exec("sed -i 's/^SSLCertificateKeyFile.*/SSLCertificateKeyFile ".str_replace("/","\/",$privkey)."/g' $ssl_apache_conf");
if(file_exists($chain)){
shell_exec("sed -i 's/^#SSLCertificateChainFile.*/SSLCertificateChainFile ".str_replace("/","\/",$chain)."/g' $ssl_apache_conf");
shell_exec("sed -i 's/^SSLCertificateChainFile.*/SSLCertificateChainFile ".str_replace("/","\/",$chain)."/g' $ssl_apache_conf");
}
shell_exec("sed -i 's/^ssl_certificate .*/".str_replace("/","\/","ssl_certificate $cert;")."/g' $ssl_nginx_conf");
shell_exec("sed -i 's/^ssl_certificate_key .*/".str_replace("/","\/","ssl_certificate_key $privkey;")."/g' $ssl_nginx_conf");
$service = trim(shell_exec("whereis service | awk '{print $2}'"));
if($flag_nginx=="On"){
shell_exec("$service nginx reload");
}else{
shell_exec("$service httpd reload");
}
}
}
else {
$now = time();
file_put_contents("/var/log/letsencrypt/$d_name.log","");
$sql_update = $zdbh->prepare("UPDATE x_ssl SET ssl_status='-2',ssl_created=:time WHERE ssl_doamin=:domain AND ssl_delete IS NULL");
$sql_update->bindParam(':domain', $d_name);
$sql_update->bindParam(':time', $now);
$sql_update->execute();
}
}
}
}
}
else
{
$sql_query = "SELECT * FROM x_ssl WHERE ssl_status='-1' AND ssl_delete IS NULL";
$sql = $zdbh->prepare($sql_query);
$sql->execute();
$point_count = $sql->rowCount();
if($point_count > 0){
while($sslrows=$sql->fetch())
{
$d_name = $sslrows['ssl_doamin'];
$server_ip = ctrl_options::GetSystemOption('server_ip');
$domain_point = '';
$output = CheckDomainisPoint($d_name);
if($output == 'no_domain_pointed'){
$domain_point = 'BADPOINTING';
}
if($domain_point == ""){
$sql_update = $zdbh->prepare("UPDATE x_ssl SET ssl_status=0 WHERE ssl_doamin=:domain AND ssl_delete IS NULL");
$sql_update->bindParam(':domain', $d_name);
$sql_update->execute();
}
else
{
echo $d_name." Not Pointed \n";
}
}
}
$sql_query = "SELECT * FROM x_ssl WHERE ssl_status='-2' AND ssl_delete IS NULL";
$sql = $zdbh->prepare($sql_query);
$sql->execute();
$point_count = $sql->rowCount();
if($point_count > 0){
shell_exec("/usr/local/letsencrypt/./certbot-auto certificates > /var/log/letsencrypt/sslrenew.log");
if(file_exists('/var/log/letsencrypt/sslrenew.log')){
$domain_certbot = shell_exec("$grep_path -ni 'Domains' /var/log/letsencrypt/sslrenew.log");
}else{
echo "no file /var/log/letsencrypt/sslrenew.log \n";
}
$line_cerbot = explode("\n",$domain_certbot);
$line=array();
$domain_list=array();
foreach($line_cerbot as $key=>$value)
{
if(trim($value)!="")
{
$line_num = explode(":", $value);
$line[] = trim($line_num[0]) + 3;
$domain_list_temp=explode(":",$value);
$domain_list_temp1=explode(",",$domain_list_temp[2]);
$domain_list[]=trim($domain_list_temp1[0]);
}
}
foreach($domain_list as $key=>$value)
{
if(trim($value)!="")
{
$domain_lists_temp=explode(" ",$value);
$domain_lists[]=trim($domain_lists_temp[0]);
}
}
while($sslrows=$sql->fetch())
{
$d_name = $sslrows['ssl_doamin'];
if((in_array(trim($d_name) ,$domain_lists))){
$sql_update = $zdbh->prepare("UPDATE x_ssl SET ssl_status=0,ssl_created=:time WHERE ssl_doamin=:domain AND ssl_delete IS NULL");
$sql_update->bindParam(':domain', $d_name);
$sql_update->bindParam(':time', $time);
$sql_update->execute();
}
else {
echo "Stating ssl_status -2 ssl_doamin $d_name \n";
$last_install_time = $sslrows['ssl_created'];
$time = time();
$seconds_diff = $time - $last_install_time;
echo "time_diff $seconds_diff\n";
if($seconds_diff > 21600){
echo "Ending ssl_status -2 ssl_doamin $d_name \n";
echo "Empty Log File \n";
shell_exec("echo \"\" > /var/log/letsencrypt/$d_name.log");
$sql_update = $zdbh->prepare("UPDATE x_ssl SET ssl_status=0,ssl_created=:time WHERE ssl_doamin=:domain AND ssl_delete IS NULL");
$sql_update->bindParam(':domain', $d_name);
$sql_update->bindParam(':time', $time);
$sql_update->execute();
}
}
}
}
}
#
# DOVECOTE AND POSTFIX MAIL SERVER SSL?TSL CONFIG
#
if(!is_dir("/etc/dovecot/domains")){
shell_exec("mkdir -p /etc/dovecot/domains");
}
$get_ssl_tls_enable=trim(shell_exec('grep -ni "ssl" /etc/dovecot/dovecot.conf | grep "yes"'));
if($get_ssl_tls_enable)
{
$sql_query = "SELECT * FROM x_vhosts";
$sql = $zdbh->prepare($sql_query);
$sql->execute();
$VhostDomCout = $sql->rowCount();
if($VhostDomCout > 0){
$line = "";
$domains = array();
$RestartSevices = false;
while($VhostDomDat = $sql->fetch(PDO::FETCH_ASSOC)){
$vh_name_vc = $VhostDomDat['vh_name_vc'];
if(empty($vh_name_vc)){
continue;
}
if(in_array($vh_name_vc, $domains)){
continue;
}
$domains[] = $vh_name_vc;
if(!is_dir("/etc/letsencrypt/live/mail.$vh_name_vc")){
if(file_exists("/etc/dovecot/domains/mail.$vh_name_vc.conf")){
unlink("/etc/dovecot/domains/mail.$vh_name_vc.conf");
}
continue;
}
$line = "\nmail.$vh_name_vc /etc/letsencrypt/live/mail.$vh_name_vc/privkey.pem /etc/letsencrypt/live/mail.$vh_name_vc/fullchain.pem";
if(shell_exec("grep '^mail.$vh_name_vc' /etc/postfix/domain_ssl.map | wc -l | tr -d '\n'") == 0){
$RestartSevices = true;
file_put_contents("/etc/postfix/domain_ssl.map", $line, FILE_APPEND);
shell_exec("/usr/sbin/postmap -F /etc/postfix/domain_ssl.map");
}
if(!file_exists("/etc/dovecot/domains/mail.$vh_name_vc.conf")){
$RestartSevices = true;
$return_mgs = "\nlocal_name mail.$vh_name_vc {\n";
$return_mgs .= "ssl_cert = /var/sentora/logs/$host_domain");
$CertFileDir = false;
if(file_exists("/var/sentora/logs/$host_domain")){
$CertFileDir = trim(shell_exec("grep 'Certificate Name' /var/sentora/logs/$host_domain | awk '{ print $3 }' | tr -d'\n'"));
}
$cert = $privkey = $chain = "";
if($CertFileDir){
$file_path = "/etc/letsencrypt/live/$CertFileDir";
$ssl_hostname = true;
$cert = "$file_path/cert.pem";
$privkey = "$file_path/privkey.pem";
$chain = "$file_path/chain.pem";
if(!file_exists($cert) || !file_exists($privkey) || !file_exists($chain)){
$ssl_hostname = false;
}
}
else
{
$sql = "SELECT * FROM x_vhosts WHERE vh_name_vc = '$host_domain' AND vh_deleted_ts IS NULL";
$sql = $zdbh->query($sql);
$sql->execute();
if($sql->rowCount() == 0 ){
$domain_point = '';
$output = CheckDomainisPoint($host_domain);
if($output == 'no_domain_pointed'){
$domain_point = 'BADPOINTING';
}
if($domain_point == "BADPOINTING"){
echo "server hostname is not pointed";
}else{
$d_mail = "admin@$host_domain";
$d_uname = "ovipanel";
$wwwoption = "without";
$dir_path = ctrl_options::GetSystemOption('ipdomain_dir');
if($dir_path != "/etc/sentora/panel/etc/static/pages/"){
$hosted_dir = ctrl_options::GetSystemOption('hosted_dir');
$hosted_dir = rtrim($hosted_dir, "/");
$hosted_dir = ltrim($hosted_dir, "/");
$hosted_dir = "/$hosted_dir/";
$d_uname = str_replace($hosted_dir, "", $dir_path);
$d_uname_arr = explode("/", $d_uname);
$d_uname = $d_uname_arr[0];
}
shell_exec("$sh_path /scripts/ssl.sh $host_domain $d_mail $wwwoption $dir_path $d_uname >> /var/sentora/logs/ssl_install_log");
}
}
}
removePidFile();
?>