function killbyports() { $sock = new sockets(); $unix = new unix(); $kill = $unix->find_program("kill"); $ufdbguardConfig = unserialize(base64_decode($sock->GET_INFO("ufdbguardConfig"))); if (!isset($datas["listen_addr"])) { $datas["listen_addr"] = "127.0.0.1"; } if (!isset($datas["listen_port"])) { $datas["listen_port"] = "3977"; } if (!isset($datas["tcpsockets"])) { $datas["tcpsockets"] = 0; } if ($datas["listen_addr"] == null) { $datas["listen_addr"] = "127.0.0.1"; } if (!is_numeric($datas["listen_port"])) { $datas["listen_port"] = "3977"; } if ($datas["tcpsockets"] == 0) { return 1; } $PIDS = $unix->PIDOF_BY_PORT($datas["listen_port"]); if (count($PIDS) == 0) { if ($GLOBALS["OUTPUT"]) { echo "Stopping......: " . date("H:i:s") . " [INIT]: {$GLOBALS["TITLENAME"]} 0 PID listens {$datas["listen_port"]}...\n"; } return; } while (list($pid, $b) = each($PIDS)) { if ($unix->process_exists($pid)) { $cmdline = @file_get_contents("/proc/{$pid}/cmdline"); if ($GLOBALS["OUTPUT"]) { echo "Stopping......: " . date("H:i:s") . " [INIT]: {$GLOBALS["TITLENAME"]} killing PID {$pid} that listens {$datas["listen_port"]} TCP port\n"; } if ($GLOBALS["OUTPUT"]) { echo "Stopping......: " . date("H:i:s") . " [INIT]: {$GLOBALS["TITLENAME"]} {$cmdline}\n"; } unix_system_kill_force($pid); } } }
function killbyports() { $sock = new sockets(); $unix = new unix(); $kill = $unix->find_program("kill"); $SyncThingPort = intval($sock->GET_INFO("SyncThingPort")); if ($SyncThingPort == 0) { $SyncThingPort = 8000; } $PIDS = $unix->PIDOF_BY_PORT($SyncThingPort); if (count($PIDS) == 0) { if ($GLOBALS["OUTPUT"]) { echo "Stopping......: " . date("H:i:s") . " [INIT]: {$GLOBALS["TITLENAME"]} 0 PID listens {$SyncThingPort}...\n"; } return; } while (list($pid, $b) = each($PIDS)) { if ($unix->process_exists($pid)) { $cmdline = @file_get_contents("/proc/{$pid}/cmdline"); if ($GLOBALS["OUTPUT"]) { echo "Stopping......: " . date("H:i:s") . " [INIT]: {$GLOBALS["TITLENAME"]} killing PID {$pid} that listens {$SyncThingPort}} TCP port\n"; } if ($GLOBALS["OUTPUT"]) { echo "Stopping......: " . date("H:i:s") . " [INIT]: {$GLOBALS["TITLENAME"]} {$cmdline}\n"; } unix_system_kill_force($pid); } } }
function fuser_port() { $unix = new unix(); $kill = $unix->find_program("kill"); build_progress_restart("{checking} TCP:5432", 66); $PIDS = $unix->PIDOF_BY_PORT("5432"); if (count($PIDS) == 0) { if ($GLOBALS["OUTPUT"]) { echo "Stopping......: " . date("H:i:s") . " [INIT]: {$GLOBALS["TITLENAME"]} 0 PID listens 5432...\n"; } return; } while (list($pid, $b) = each($PIDS)) { if ($unix->process_exists($pid)) { build_progress_restart("{killing} {$pid} : 5432", 67); $cmdline = @file_get_contents("/proc/{$pid}/cmdline"); if ($GLOBALS["OUTPUT"]) { echo "Stopping......: " . date("H:i:s") . " [INIT]: {$GLOBALS["TITLENAME"]} killing PID {$pid} that listens 53 UDP port\n"; } if ($GLOBALS["OUTPUT"]) { echo "Stopping......: " . date("H:i:s") . " [INIT]: {$GLOBALS["TITLENAME"]} {$cmdline}\n"; } unix_system_kill_force($pid); } } }
function fuser_port($port) { $unix = new unix(); $kill = $unix->find_program("kill"); $PIDS = $unix->PIDOF_BY_PORT($port); if (count($PIDS) == 0) { if ($GLOBALS["OUTPUT"]) { echo "Stopping......: " . date("H:i:s") . " [INIT]: {$GLOBALS["SERVICE_NAME"]} 0 PID listens {$port}...\n"; } return; } while (list($pid, $b) = each($PIDS)) { if ($unix->process_exists($pid)) { if ($GLOBALS["OUTPUT"]) { echo "Stopping......: " . date("H:i:s") . " [INIT]: {$GLOBALS["SERVICE_NAME"]} killing PID {$pid} that listens {$port}\n"; } unix_system_kill_force($pid); } } }
function start_squid($aspid = false) { $GLOBALS["LOGS"] = array(); $suffix = null; if ($GLOBALS["MONIT"]) { $suffix = " (by system monitor)"; } if ($GLOBALS["BY_CACHE_LOGS"]) { $suffix = " (by cache.log monitor)"; } if ($GLOBALS["BY_STATUS"]) { $suffix = " (by Artica monitor)"; } if ($GLOBALS["BY_CLASS_UNIX"]) { $suffix = " (by Artica class.unix.inc)"; } if ($GLOBALS["BY_FRAMEWORK"]) { $suffix = " (by Artica framework)"; } if ($GLOBALS["BY_OTHER_SCRIPT"]) { $suffix = " (by other script)"; } if ($GLOBALS["BY_ARTICA_INSTALL"]) { $suffix = " (by artica-install)"; } if ($GLOBALS["BY_FORCE_RECONFIGURE"]) { $suffix = " (after building settings)"; } $unix = new unix(); $php = $unix->LOCATE_PHP5_BIN(); $nohup = $unix->find_program("nohup"); $sock = new sockets(); $reconfigure = false; $SQUIDEnable = $sock->GET_INFO("SQUIDEnable"); $NtpdateAD = $sock->GET_INFO("NtpdateAD"); if (!is_numeric($SQUIDEnable)) { $SQUIDEnable = 1; } $kill = $unix->find_program("kill"); if (!is_numeric($NtpdateAD)) { $NtpdateAD = 0; } $su_bin = $unix->find_program("su"); $sysctl = $unix->find_program("sysctl"); $squidbin = $unix->LOCATE_SQUID_BIN(); if (!is_file($squidbin)) { if ($GLOBALS["OUTPUT"]) { echo "Restart......: Squid-cache, not installed\n"; } return; } if ($GLOBALS["MONIT"]) { if (function_exists("debug_backtrace")) { $trace = debug_backtrace(); if (isset($trace[1])) { $file = basename($trace[1]["file"]); $function = $trace[1]["function"]; $line = $trace[1]["line"]; $called = "Called by {$function}() from line {$line}"; } } $pid = SQUID_PID(); if ($unix->process_exists($pid)) { $ps = $unix->find_program("ps"); $grep = $unix->find_program("grep"); exec("{$ps} aux|{$grep} squid 2>&1", $results); squid_admin_mysql(2, "Monit ordered to start squid but squid is still in memory PID {$pid} ??", "I cannot accept this order, see details\n" . @implode("\n", $results), __FILE__, __LINE__); $squidpidfile = $unix->LOCATE_SQUID_PID(); @file_put_contents($squidpidfile, $pid); return; } squid_admin_mysql(1, "Monit ordered to start squid", $called, __FILE__, __LINE__); } if ($SQUIDEnable == 0) { if ($GLOBALS["OUTPUT"]) { echo "Starting......: " . date("H:i:s") . " Squid is disabled...\n"; } return; } if (is_file("/etc/artica-postfix/squid.lock")) { $time = $unix->file_time_min("/etc/artica-postfix/squid.lock"); if ($time < 60) { if ($GLOBALS["OUTPUT"]) { echo "Starting......: " . date("H:i:s") . " Squid is locked (since {$time}Mn...\n"; } return; } @unlink("/etc/artica-postfix/squid.lock"); } $pids = $unix->PIDOF_PATTERN_ALL("exec.squid.watchdog.php --start"); if (count($pids) > 2) { if ($GLOBALS["OUTPUT"]) { echo "Starting......: " . date("H:i:s") . " Too many instances " . count($pids) . " starting squid, kill them!\n"; } $mypid = getmypid(); while (list($pid, $ligne) = each($pids)) { if ($pid == $mypid) { continue; } if ($GLOBALS["OUTPUT"]) { echo "Starting......: " . date("H:i:s") . " killing {$pid}\n"; } unix_system_kill_force($pid); } } if (!$aspid) { $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid"; $pid = $unix->get_pid_from_file($pidfile); if ($unix->process_exists($pid, basename(__FILE__))) { $time = $unix->PROCCESS_TIME_MIN($pid); if ($time < 5) { Events("Task Already running PID {$pid} since {$time}mn"); if ($GLOBALS["OUTPUT"]) { echo "Starting......: " . date("H:i:s") . " Already task running PID {$pid} since {$time}mn, Aborting operation (" . __LINE__ . ")\n"; } return; } squid_admin_mysql(0, "Too long time for artica task PID {$pid} running since {$time}mn", "Process will be killed"); Tosyslog("Too long time for artica task PID {$pid} running since {$time}mn -> kill"); unix_system_kill_force($pid); } @file_put_contents($pidfile, getmypid()); } $squidbin = $unix->find_program("squid"); if (!is_file($squidbin)) { $squidbin = $unix->find_program("squid3"); } if (!is_file($squidbin)) { system_admin_events("Squid not seems to be installed", __FUNCTION__, __FILE__, __LINE__, "proxy"); return; } @chmod($squidbin, 0755); $sock = new sockets(); $DisableForceFCK = intval($sock->GET_INFO("DisableForceFCK")); if ($DisableForceFCK == 0) { if ($GLOBALS["OUTPUT"]) { echo "Starting......: " . date("H:i:s") . " Will force a checkdisk At next reboot\n"; } @touch("/forcefsck"); } start_prepare(); $pid = SQUID_PID(); if ($GLOBALS["CRASHED"]) { for ($i = 0; $i < 10; $i++) { sleep(1); $pid = SQUID_PID(); if ($unix->process_exists($pid)) { continue; } break; } squid_admin_mysql(2, "No need to start Proxy service after a crash", "It seems the watchdog detect a crash but after 10s the proxy still running\nOperation is aborted", __FILE__, __LINE__); return; } if ($unix->process_exists($pid)) { $time = $unix->PROCCESS_TIME_MIN($pid); if ($GLOBALS["OUTPUT"]) { echo "Starting......: " . date("H:i:s") . " Proxy service already running pid {$pid} since {$time}mn\n"; } system_admin_events("Squid seems to already running pid {$pid} since {$time}mn", __FUNCTION__, __FILE__, __LINE__, "proxy"); return; } $unix->TCP_TUNE_SQUID_DEFAULT(); $t1 = time(); SendLogs("Checking caches..."); $cacheBooster = new squidbooster(); $cacheBooster->cache_booster(); CheckStoreDirs(true); SendLogs("Checking caches done..."); SendLogs("Checking Ports..."); $array = CheckAllports(); SendLogs("Checking " . count($array) . " ports"); while (list($port, $ligne) = each($array)) { $portZ = $unix->PIDOF_BY_PORT($port); SendLogs("Checking port {$port} - " . count($portZ) . " process(es)"); if (count($portZ) > 0) { while (list($pid, $ligne) = each($portZ)) { SendLogs("Checking port {$port} - killing pid {$pid}"); shell_exec("kill -9 {$pid} >/dev/null 2>&1"); } } } SendLogs("Starting squid {$squidbin}...."); $echo = $unix->find_program("echo"); $size = round(@filesize("/var/log/squid/cache.log") / 1024, 2) / 1024; if ($size > 10) { @copy("/var/log/squid/cache.log", "/var/log/squid/cache.log." . time()); shell_exec("{$echo} \" \"> /var/log/squid/cache.log 2>&1"); } @chmod($squidbin, 0755); @chmod("/var/log/squid", 0755); if (is_link("/var/log/squid")) { @chmod(readlink("/var/log/squid"), 0755); } squid_admin_mysql(1, "Starting Squid-cache service {$suffix}", @implode("\n", $GLOBALS["LOGS"]), __FILE__, __LINE__); exec("{$squidbin} -f /etc/squid3/squid.conf 2>&1", $GLOBALS["LOGS"]); $PRC = 40; $MAXPRC = 60; $AB = 0; for ($i = 0; $i < 10; $i++) { $PRC++; if ($PRC > $MAXPRC - 1) { $PRC = $MAXPRC - 1; } build_progress_restart("{starting_service}", $PRC); $pid = SQUID_PID(); if ($unix->process_exists($pid)) { SendLogs("Starting squid started pid {$pid}..."); break; } ToSyslog("Starting squid waiting {$i}/10s"); SendLogs("Starting squid waiting {$i}/10s"); sleep(1); } if (!$unix->process_exists($pid)) { SendLogs("Starting Squid failed to start..."); ToSyslog("Starting Squid failed to start..."); if (function_exists("debug_backtrace")) { $trace = debug_backtrace(); if (isset($trace[1])) { $sourcefunction = $trace[1]["function"]; $sourceline = $trace[1]["line"]; $executed = "Executed by {$sourcefunction}() line {$sourceline}\nusing argv:{$GLOBALS["ARGVS"]}\n"; } } squid_admin_mysql(0, "Squid failed to start {$suffix}", @implode("\n", $GLOBALS["LOGS"]) . "\n{$executed}"); squid_admin_notifs("Starting Squid failed to start\n" . @implode("\n", $GLOBALS["LOGS"]) . "\n{$executed}", __FUNCTION__, __FILE__, __LINE__, "proxy"); system_admin_events("Starting Squid failed to start\n" . @implode("\n", $GLOBALS["LOGS"]), __FUNCTION__, __FILE__, __LINE__, "proxy"); return; } SendLogs("Starting Squid Tests if it listen all connections...."); for ($i = 0; $i < 10; $i++) { if (is_started()) { SendLogs("Starting squid listen All connections OK"); break; } SendLogs("Starting squid listen All connections... waiting {$i}/10"); sleep(1); } $took = $unix->distanceOfTimeInWords($t1, time()); $nohup = $unix->find_program("nohup"); SendLogs("Starting Squid success to start PID {$pid}..."); if (function_exists("debug_backtrace")) { $trace = debug_backtrace(); if (isset($trace[1])) { $sourcefunction = $trace[1]["function"]; $sourceline = $trace[1]["line"]; $executed = "Executed by {$sourcefunction}() line {$sourceline}\nusing argv:{$GLOBALS["ARGVS"]}\n"; } } $php5 = $unix->LOCATE_PHP5_BIN(); system_admin_events("Starting Squid success to start PID {$pid} took {$took}\n" . @implode("\n", $GLOBALS["LOGS"]), __FUNCTION__, __FILE__, __LINE__, "proxy"); SendLogs("Starting Squid finishing Reloading DNSMasq"); shell_exec("{$nohup} /etc/init.d/dnsmasq reload >/dev/null 2>&1 &"); SendLogs("Starting Squid finishing Starting Squid-cache tail"); shell_exec("{$nohup} /etc/init.d/artica-postfix start squidcache-tail >/dev/null 2>&1 &"); SendLogs("Starting Squid finishing Starting auth-cache tail"); shell_exec("{$nohup} /etc/init.d/auth-tail restart >/dev/null 2>&1 &"); SendLogs("Starting Squid finishing Reconfiguring proxy.pac scripts"); shell_exec("{$nohup} {$php5} " . basename(__FILE__) . "/exec.proxy.pac.php --write 2>&1 &"); SendLogs("Starting Squid done..."); shell_exec("{$nohup} {$php5} " . basename(__FILE__) . "/exec.cache.pages.php --force >/dev/null 2>&1 &"); }
function start($aspid = false) { $unix = new unix(); $sock = new sockets(); if (!$aspid) { $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid"; $pid = $unix->get_pid_from_file($pidfile); if ($unix->process_exists($pid, basename(__FILE__))) { $time = $unix->PROCCESS_TIME_MIN($pid); if ($GLOBALS["OUTPUT"]) { echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["SERVICE_NAME"]} Already Artica task running PID {$pid} since {$time}mn\n"; } return; } @file_put_contents($pidfile, getmypid()); } $ROUNDCUBE_MAIN_FOLDER = ROUNDCUBE_MAIN_FOLDER(); if (!is_dir(ROUNDCUBE_MAIN_FOLDER())) { if ($GLOBALS["OUTPUT"]) { echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["SERVICE_NAME"]} not installed\n"; } return; } $RoundCubeHTTPEngineEnabled = intval($sock->GET_INFO("RoundCubeHTTPEngineEnabled")); $pid = LIGHTTPD_PID(); if ($RoundCubeHTTPEngineEnabled == 0) { if ($GLOBALS["OUTPUT"]) { echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["SERVICE_NAME"]} disabled (RoundCubeHTTPEngineEnabled)..\n"; } if ($unix->process_exists($pid)) { stop(true); } return; } if ($unix->process_exists($pid)) { $timepid = $unix->PROCCESS_TIME_MIN($pid); if ($GLOBALS["OUTPUT"]) { echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["SERVICE_NAME"]} {$GLOBALS["SERVICE_NAME"]} already started {$pid} since {$timepid}Mn...\n"; } return; } $nohup = $unix->find_program("nohup"); $php5 = $unix->LOCATE_PHP5_BIN(); $apache2ctl = $unix->LOCATE_APACHE_CTL(); $LIGHTTPD_CONF_PATH = LIGHTTPD_CONF_PATH(); $RoundCubeHTTPSPort = intval($sock->GET_INFO("RoundCubeHTTPSPort")); $RoundCubeHTTPPort = intval($sock->GET_INFO("RoundCubeHTTPPort")); $RoundCubeUseSSL = intval($sock->GET_INFO("RoundCubeUseSSL")); if ($RoundCubeHTTPSPort == 0) { $RoundCubeHTTPSPort = 449; } if ($RoundCubeHTTPPort == 0) { $RoundCubeHTTPPort = 8888; } if (!is_file("/opt/artica/ssl/certs/lighttpd.pem")) { if ($GLOBALS["OUTPUT"]) { echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["SERVICE_NAME"]} creating SSL certificate..\n"; } exec("/usr/share/artica-postfix/bin/artica-install -lighttpd-cert 2>&1", $results); while (list($num, $line) = each($results)) { if ($GLOBALS["OUTPUT"]) { echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["SERVICE_NAME"]} {$line}\n"; } } } apache_config(); if ($RoundCubeUseSSL == 1) { if ($GLOBALS["OUTPUT"]) { echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["SERVICE_NAME"]} Get PID from PORT HTTPS/TCP:{$RoundCubeHTTPSPort}\n"; } $pids = $unix->PIDOF_BY_PORT($RoundCubeHTTPSPort); if (count($pids) > 0) { while (list($pid, $line) = each($pids)) { if ($GLOBALS["OUTPUT"]) { echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["SERVICE_NAME"]} kill PID {$pid} that listens {$RoundCubeHTTPSPort}\n"; } $unix->KILL_PROCESS($pid, 9); } } } if ($GLOBALS["OUTPUT"]) { echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["SERVICE_NAME"]} Get PID from PORT HTTP/TCP:{$RoundCubeHTTPPort}\n"; } $pids = $unix->PIDOF_BY_PORT($RoundCubeHTTPPort); if (count($pids) > 0) { while (list($pid, $line) = each($pids)) { if ($GLOBALS["OUTPUT"]) { echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["SERVICE_NAME"]} kill PID {$pid} that listens {$RoundCubeHTTPPort}\n"; } $unix->KILL_PROCESS($pid, 9); } } $cmd = "{$apache2ctl} -f {$LIGHTTPD_CONF_PATH} -k start"; if ($GLOBALS["VERBOSE"]) { echo "{$cmd}\n"; } shell_exec($cmd); for ($i = 0; $i < 6; $i++) { $pid = LIGHTTPD_PID(); if ($unix->process_exists($pid)) { break; } if ($GLOBALS["OUTPUT"]) { echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["SERVICE_NAME"]} waiting {$i}/6...\n"; } sleep(1); } $pid = LIGHTTPD_PID(); if ($unix->process_exists($pid)) { if ($GLOBALS["OUTPUT"]) { echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["SERVICE_NAME"]} Success service started pid:{$pid}...\n"; } } else { if ($GLOBALS["OUTPUT"]) { echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["SERVICE_NAME"]} failed...\n"; } if ($GLOBALS["OUTPUT"]) { echo "Starting......: " . date("H:i:s") . " [INIT]: {$cmd}\n"; } } }
function start_squid($aspid = false) { $GLOBALS["LOGS"] = array(); $suffix = null; if ($GLOBALS["MONIT"]) { $suffix = " (by system monitor)"; } if ($GLOBALS["BY_CACHE_LOGS"]) { $suffix = " (by cache.log monitor)"; } if ($GLOBALS["BY_STATUS"]) { $suffix = " (by Artica monitor)"; } if ($GLOBALS["BY_CLASS_UNIX"]) { $suffix = " (by Artica class.unix.inc)"; } if ($GLOBALS["BY_FRAMEWORK"]) { $suffix = " (by Artica framework)"; } if ($GLOBALS["BY_OTHER_SCRIPT"]) { $suffix = " (by other script)"; } if ($GLOBALS["BY_ARTICA_INSTALL"]) { $suffix = " (by artica-install)"; } if ($GLOBALS["BY_FORCE_RECONFIGURE"]) { $suffix = " (after building settings)"; } $unix = new unix(); $php = $unix->LOCATE_PHP5_BIN(); $nohup = $unix->find_program("nohup"); $sock = new sockets(); $reconfigure = false; $SQUIDEnable = $sock->GET_INFO("SQUIDEnable"); $NtpdateAD = $sock->GET_INFO("NtpdateAD"); if (!is_numeric($SQUIDEnable)) { $SQUIDEnable = 1; } $kill = $unix->find_program("kill"); if (!is_numeric($NtpdateAD)) { $NtpdateAD = 0; } $su_bin = $unix->find_program("su"); $sysctl = $unix->find_program("sysctl"); $squidbin = $unix->LOCATE_SQUID_BIN(); if (!is_file($squidbin)) { build_progress_start("Not installed", 110); if ($GLOBALS["OUTPUT"]) { echo "Restart......: Squid-cache, not installed\n"; } return; } if ($GLOBALS["MONIT"]) { if (function_exists("debug_backtrace")) { $trace = debug_backtrace(); if (isset($trace[1])) { $file = basename($trace[1]["file"]); $function = $trace[1]["function"]; $line = $trace[1]["line"]; $called = "Called by {$function}() from line {$line}"; } } $pid = SQUID_PID(); if ($unix->process_exists($pid)) { $ps = $unix->find_program("ps"); $grep = $unix->find_program("grep"); exec("{$ps} aux|{$grep} squid 2>&1", $results); squid_admin_mysql(2, "Monit ordered to start squid but squid is still in memory PID {$pid} ??", "I cannot accept this order, see details\n" . @implode("\n", $results), __FILE__, __LINE__); $squidpidfile = $unix->LOCATE_SQUID_PID(); @file_put_contents($squidpidfile, $pid); return; } squid_admin_mysql(1, "Monit ordered to start squid", $called, __FILE__, __LINE__); } if ($SQUIDEnable == 0) { if ($GLOBALS["OUTPUT"]) { echo "Starting......: " . date("H:i:s") . " Squid is disabled...\n"; } build_progress_start("Proxy service is disabled", 110); return; } if (is_file("/etc/init.d/iptables-transparent")) { shell_exec("/etc/init.d/iptables-transparent start"); } if (is_file("/etc/artica-postfix/squid.lock")) { $time = $unix->file_time_min("/etc/artica-postfix/squid.lock"); if ($time < 60) { if ($GLOBALS["OUTPUT"]) { echo "Starting......: " . date("H:i:s") . " Proxy is locked (since {$time}Mn...\n"; } build_progress_start(" Proxy is locked (since {$time}Mn", 110); return; } @unlink("/etc/artica-postfix/squid.lock"); } $pids = $unix->PIDOF_PATTERN_ALL("exec.squid.watchdog.php --start"); if (count($pids) > 2) { if ($GLOBALS["OUTPUT"]) { echo "Starting......: " . date("H:i:s") . " Too many instances " . count($pids) . " starting squid, kill them!\n"; } $mypid = getmypid(); while (list($pid, $ligne) = each($pids)) { if ($pid == $mypid) { continue; } if ($GLOBALS["OUTPUT"]) { echo "Starting......: " . date("H:i:s") . " killing {$pid}\n"; } unix_system_kill_force($pid); } } if (!$aspid) { $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid"; $pid = $unix->get_pid_from_file($pidfile); if ($unix->process_exists($pid, basename(__FILE__))) { $time = $unix->PROCCESS_TIME_MIN($pid); if ($time < 5) { build_progress_start("Task Already running PID {$pid} since {$time}mn", 110); Events("Task Already running PID {$pid} since {$time}mn"); if ($GLOBALS["OUTPUT"]) { echo "Starting......: " . date("H:i:s") . " Already task running PID {$pid} since {$time}mn, Aborting operation (" . __LINE__ . ")\n"; } return; } squid_admin_mysql(0, "Too long time for artica task PID {$pid} running since {$time}mn", "Process will be killed"); Tosyslog("Too long time for artica task PID {$pid} running since {$time}mn -> kill"); unix_system_kill_force($pid); } @file_put_contents($pidfile, getmypid()); } $squidbin = $unix->find_program("squid"); if (!is_file($squidbin)) { $squidbin = $unix->find_program("squid3"); } if (!is_file($squidbin)) { build_progress_start("Not installed", 110); system_admin_events("Squid not seems to be installed", __FUNCTION__, __FILE__, __LINE__, "proxy"); return; } @chmod($squidbin, 0755); $sock = new sockets(); $DisableForceFCK = intval($sock->GET_INFO("DisableForceFCK")); if ($DisableForceFCK == 0) { if ($GLOBALS["OUTPUT"]) { echo "Starting......: " . date("H:i:s") . " Will force a checkdisk At next reboot\n"; } @touch("/forcefsck"); } $pid = SQUID_PID(); if ($unix->process_exists($pid)) { $time = $unix->PROCCESS_TIME_MIN($pid); build_progress_start("Proxy service already running since {$time}Mn", 50); if ($GLOBALS["START_PROGRESS"]) { $php = $unix->LOCATE_PHP5_BIN(); build_progress_start("Removing caches...", 55); @unlink("/usr/share/artica-postfix/ressources/databases/ALL_SQUID_STATUS"); build_progress_start("Building caches...", 70); system("{$php} /usr/share/artica-postfix/exec.status.php --all-squid"); if (!is_file("/usr/share/artica-postfix/ressources/databases/ALL_SQUID_STATUS")) { build_progress_start("Fatal!! Watchdog issue!!", 110); return; } build_progress_start("{done}", 100); } return; } build_progress_start("Preparing proxy service", 50); start_prepare(); $squid_checks = new squid_checks(); $squid_checks->squid_parse(); build_progress_start("{starting_proxy_service}", 60); $pid = SQUID_PID(); if ($GLOBALS["CRASHED"]) { for ($i = 0; $i < 10; $i++) { sleep(1); $pid = SQUID_PID(); if ($unix->process_exists($pid)) { continue; } break; } squid_admin_mysql(2, "No need to start Proxy service after a crash", "It seems the watchdog detect a crash but after 10s the proxy still running\nOperation is aborted", __FILE__, __LINE__); return; } build_progress_start("Tuning network", 70); $unix->TCP_TUNE_SQUID_DEFAULT(); $t1 = time(); build_progress_start("Checking caches", 71); SendLogs("Checking caches..."); $cacheBooster = new squidbooster(); $cacheBooster->cache_booster(); build_progress_start("Checking caches", 73); CheckStoreDirs(true); SendLogs("Checking caches done..."); build_progress_start("Checking Ports", 75); SendLogs("Checking Ports..."); $array = CheckAllports(); SendLogs("Checking " . count($array) . " ports"); while (list($port, $ligne) = each($array)) { $portZ = $unix->PIDOF_BY_PORT($port); SendLogs("Checking port {$port} - " . count($portZ) . " process(es)"); if (count($portZ) > 0) { while (list($pid, $ligne) = each($portZ)) { SendLogs("Checking port {$port} - killing pid {$pid}"); shell_exec("kill -9 {$pid} >/dev/null 2>&1"); } } } build_progress_start("Checking SHM", 75); system("{$php} /usr/share/artica-postfix/exec.squid.smp.php"); SendLogs("Starting squid {$squidbin}...."); $echo = $unix->find_program("echo"); $size = round(@filesize("/var/log/squid/cache.log") / 1024, 2) / 1024; if ($size > 50) { squid_admin_mysql(2, "Cleaning cache.log {$size}MB", null, __FILE__, __LINE__); @copy("/var/log/squid/cache.log", "/var/log/squid/cache.log." . time()); shell_exec("{$echo} \" \"> /var/log/squid/cache.log 2>&1"); } @chmod($squidbin, 0755); @chmod("/var/log/squid", 0755); if (is_link("/var/log/squid")) { @chmod(readlink("/var/log/squid"), 0755); } squid_admin_mysql(1, "Starting Squid-cache service {$suffix}", @implode("\n", $GLOBALS["LOGS"]), __FILE__, __LINE__); build_progress_start("Remove SystemV5 Memory", 80); kill_shm(); CHECK_WIFIDOG_IPTABLES_RULES(); $PIDFILE = $unix->LOCATE_SQUID_PID(); $f = array(); $f[] = "#! /bin/sh"; $f[] = ". /lib/lsb/init-functions"; $f[] = "PATH=/bin:/usr/bin:/sbin:/usr/sbin"; $f[] = "DAEMON=\"{$squidbin}\""; $f[] = "CONFIG=\"/etc/squid3/squid.conf\""; $f[] = "SQUID_ARGS=\"-YC -f \$CONFIG\""; $f[] = "PIDFILE=\"{$PIDFILE}\""; $f[] = ""; $f[] = "KRB5RCACHETYPE=none"; $f[] = "KRB5_KTNAME=/etc/squid3/PROXY.keytab"; $f[] = "export KRB5RCACHETYPE"; $f[] = "export KRB5_KTNAME"; $f[] = ""; $f[] = ""; $f[] = "umask 027"; $f[] = "ulimit -n 65535"; $f[] = "start-stop-daemon --start --pidfile \$PIDFILE --exec \$DAEMON -- \$SQUID_ARGS"; $f[] = "status=\$?"; $f[] = "if [ \$status -eq 0 ]"; $f[] = "\tthen"; $f[] = "\t echo \"Success starting Proxy service\""; $f[] = "\tfi "; $f[] = "exit 0\n"; @file_put_contents("/usr/sbin/squid-start", @implode("\n", $f)); $f = array(); @chmod("/usr/sbin/squid-start", 0755); exec("/usr/sbin/squid-start 2>&1", $GLOBALS["LOGS"]); $PRC = 40; $MAXPRC = 60; $AB = 0; $TESTFAILED = false; while (list($index, $line) = each($GLOBALS["LOGS"])) { if (preg_match("#FATAL: Bungled#", $line)) { squid_admin_mysql(1, "Alert: Bungled configuration when starting Proxy", $line, __FILE__, __LINE__); $TESTFAILED = true; break; } } if ($TESTFAILED) { $TESTFAILED = false; if (!is_file("/etc/artica-postfix/SQUID_TEST_FAILED")) { build_progress_start("Reconfigure Proxy service", 80); system("{$php} /usr/share/artica-postfix/exec.squid.php --build --force"); } $GLOBALS["LOGS"] = array(); exec("{$squidbin} -f /etc/squid3/squid.conf 2>&1", $GLOBALS["LOGS"]); while (list($index, $line) = each($GLOBALS["LOGS"])) { if (preg_match("#FATAL: Bungled#", $line)) { squid_admin_mysql(1, "Alert: Bungled configuration after reconfiguring Proxy", $line, __FILE__, __LINE__); $TESTFAILED = true; break; } } } if ($TESTFAILED) { @touch("/etc/artica-postfix/SQUID_TEST_FAILED"); build_progress_start("Start Proxy service {failed}", 110); die; } @unlink("/etc/artica-postfix/SQUID_TEST_FAILED"); for ($i = 0; $i < 10; $i++) { $PRC++; if ($PRC > $MAXPRC - 1) { $PRC = $MAXPRC - 1; } build_progress_start("{starting_service} {$i}/10", 85); build_progress_restart("{starting_service}", $PRC); $pid = SQUID_PID(); if ($unix->process_exists($pid)) { SendLogs("Starting squid started pid {$pid}..."); break; } ToSyslog("Starting squid waiting {$i}/10s"); SendLogs("Starting squid waiting {$i}/10s"); sleep(1); } if (!$unix->process_exists($pid)) { build_progress_start("{failed}", 110); SendLogs("Starting Squid failed to start..."); ToSyslog("Starting Squid failed to start..."); if (function_exists("debug_backtrace")) { $trace = debug_backtrace(); if (isset($trace[1])) { $sourcefunction = $trace[1]["function"]; $sourceline = $trace[1]["line"]; $executed = "Executed by {$sourcefunction}() line {$sourceline}\nusing argv:{$GLOBALS["ARGVS"]}\n"; } } squid_admin_mysql(0, "Squid failed to start {$suffix}", @implode("\n", $GLOBALS["LOGS"]) . "\n{$executed}"); system_admin_events("Starting Squid failed to start\n" . @implode("\n", $GLOBALS["LOGS"]), __FUNCTION__, __FILE__, __LINE__, "proxy"); return; } SendLogs("Starting Squid Tests if it listen all connections...."); for ($i = 0; $i < 10; $i++) { build_progress_start("{checking} {$i}/10", 90); if (is_started()) { SendLogs("Starting squid listen All connections OK"); break; } SendLogs("Starting squid listen All connections... waiting {$i}/10"); sleep(1); } $took = $unix->distanceOfTimeInWords($t1, time()); $nohup = $unix->find_program("nohup"); SendLogs("Starting Squid success to start PID {$pid}..."); if (function_exists("debug_backtrace")) { $trace = debug_backtrace(); if (isset($trace[1])) { $sourcefunction = $trace[1]["function"]; $sourceline = $trace[1]["line"]; $executed = "Executed by {$sourcefunction}() line {$sourceline}\nusing argv:{$GLOBALS["ARGVS"]}\n"; } } $php5 = $unix->LOCATE_PHP5_BIN(); taskset(); build_progress_start("Restarting cache-tail", 91); shell_exec("{$nohup} /etc/init.d/cache-tail restart >/dev/null 2>&1 &"); build_progress_start("Restarting access-tail", 92); shell_exec("{$nohup} /etc/init.d/squid-tail restart >/dev/null 2>&1 &"); build_progress_start("Restarting auth-tail", 93); shell_exec("{$nohup} /etc/init.d/auth-tail restart >/dev/null 2>&1 &"); build_progress_start("{done}", 100); system_admin_events("Starting Squid success to start PID {$pid} took {$took}\n" . @implode("\n", $GLOBALS["LOGS"]), __FUNCTION__, __FILE__, __LINE__, "proxy"); SendLogs("Starting Squid done..."); if (is_file("/usr/share/artica-postfix/ressources/databases/ALL_SQUID_STATUS")) { @unlink("/usr/share/artica-postfix/ressources/databases/ALL_SQUID_STATUS"); } }
function start($aspid = false) { $unix = new unix(); $sock = new sockets(); $Masterbin = $unix->find_program("rdpproxy"); if (!is_file($Masterbin)) { if ($GLOBALS["OUTPUT"]) { echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["TITLENAME"]}, rdpproxy not installed\n"; } return; } if (!$aspid) { $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid"; $pid = $unix->get_pid_from_file($pidfile); if ($unix->process_exists($pid, basename(__FILE__))) { $time = $unix->PROCCESS_TIME_MIN($pid); if ($GLOBALS["OUTPUT"]) { echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["TITLENAME"]} Already Artica task running PID {$pid} since {$time}mn\n"; } return; } @file_put_contents($pidfile, getmypid()); } $pid = PID_NUM(); if ($unix->process_exists($pid)) { $timepid = $unix->PROCCESS_TIME_MIN($pid); if ($GLOBALS["OUTPUT"]) { echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["TITLENAME"]} Service already started {$pid} since {$timepid}Mn...\n"; } return; } $EnableRDPProxy = $sock->GET_INFO("EnableRDPProxy"); if (!is_numeric($EnableRDPProxy)) { $EnableRDPProxy = 0; } if ($EnableRDPProxy == 0) { if ($GLOBALS["OUTPUT"]) { echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["TITLENAME"]} service disabled (see EnableRDPProxy)\n"; } return; } $nohup = $unix->find_program("nohup"); $kill = $unix->find_program("kill"); $RDPProxyPort = $sock->GET_INFO("RDPProxyPort"); if (!is_numeric($RDPProxyPort)) { $RDPProxyPort = 3389; } $PIDS = $unix->PIDOF_BY_PORT($RDPProxyPort); if (count($PIDS) == 0) { if ($GLOBALS["OUTPUT"]) { echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["TITLENAME"]} 0 PID listens {$RDPProxyPort}...\n"; } } if (count($PIDS) > 0) { while (list($pid, $b) = each($PIDS)) { if ($unix->process_exists($pid)) { $cmdline = @file_get_contents("/proc/{$pid}/cmdline"); if ($GLOBALS["OUTPUT"]) { echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["TITLENAME"]} killing PID {$pid} that listens {$RDPProxyPort} TCP port\n"; } if ($GLOBALS["OUTPUT"]) { echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["TITLENAME"]} Process: `{$cmdline}`\n"; } unix_system_kill_force($pid); } } } @mkdir('/etc/rdpproxy/cert/rdp', 0755, true); @mkdir("/var/rdpproxy/recorded", 0755, true); @mkdir("/var/run/redemption", 0755, true); @mkdir("/tmp/rdpproxy", 0755, true); @mkdir("/home/rdpproxy/recorded", 0755, true); foreach (glob("/usr/share/artica-postfix/img/rdpproxy/*") as $filename) { if ($GLOBALS["VERBOSE"]) { echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["TITLENAME"]} \"" . basename($filename) . "\"\n"; } @copy($filename, "/usr/local/share/rdpproxy/" . basename($filename)); } if (is_file("/var/run/redemption/rdpproxy.pid")) { @unlink("/var/run/redemption/rdpproxy.pid"); } $VERSION = VERSION(); $cmd = "{$nohup} {$Masterbin} >/dev/null 2>&1 &"; if ($GLOBALS["OUTPUT"]) { echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["TITLENAME"]} service v.{$VERSION}\n"; } shell_exec($cmd); for ($i = 1; $i < 5; $i++) { if ($GLOBALS["OUTPUT"]) { echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["TITLENAME"]} waiting {$i}/5\n"; } sleep(1); $pid = PID_NUM(); if ($unix->process_exists($pid)) { break; } } $pid = PID_NUM(); if ($unix->process_exists($pid)) { if ($GLOBALS["OUTPUT"]) { echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["TITLENAME"]} Success PID {$pid}\n"; } if (!is_file("/var/run/redemption/rdpproxy.pid")) { @file_put_contents("/var/run/redemption/rdpproxy.pid", $pid); } AUTHHOOK_START(true); } else { if ($GLOBALS["OUTPUT"]) { echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["TITLENAME"]} Failed\n"; } if ($GLOBALS["OUTPUT"]) { echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["TITLENAME"]} {$cmd}\n"; } } }