Example #1
0
function remove_session()
{
    $mac = $_GET["MAC"];
    $ip = $_GET["ip"];
    writelogs_framework("MAC: {$mac}, IP={$ip} ", __FUNCTION__, __FILE__, __LINE__);
    $unix = new unix();
    $tmp = $unix->TEMP_DIR();
    $iptables_save = $unix->find_program("iptables-save");
    $iptables_restore = $unix->find_program("iptables-restore");
    shell_exec("{$iptables_save} > {$GLOBALS["CACHE_FILE"]}");
    $data = file_get_contents($GLOBALS["CACHE_FILE"]);
    $datas = explode("\n", $data);
    $c = 0;
    $tab = array();
    while (list($num, $ligne) = each($datas)) {
        if ($ligne == null) {
            continue;
        }
        if ($mac != null) {
            if (preg_match("#{$mac}#i", $ligne)) {
                $c++;
                continue;
            }
        }
        if ($ip != null) {
            if (preg_match("#{$ip}#i", $ligne)) {
                $c++;
                continue;
            }
        }
        $tab[] = $ligne . "\n";
    }
    $t = time();
    file_put_contents("{$tmp}/{$t}.conf", @implode("\n", $tab));
    system("{$iptables_restore} < {$tmp}/{$t}.conf");
    @unlink("{$tmp}/{$t}.conf");
    shell_exec("{$iptables_save} > {$GLOBALS["CACHE_FILE"]}");
    $conntrack = $unix->find_program("conntrack");
    if ($ip != null) {
        shell_exec("{$conntrack} -D -s {$ip}");
    }
    $nohup = $unix->find_program("nohup");
    $php = $unix->LOCATE_PHP5_BIN();
    writelogs_framework("Framework: Remove firewall session {$mac}/{$ip}", __FUNCTION__, __FILE__, __LINE__);
    hotspot_admin_mysql(2, "Framework: Remove firewall session {$mac}/{$ip}", "Remove firewall session {$mac}/{$ip}");
    shell_exec("{$nohup} {$php} /usr/share/artica-postfix/exec.artica.hotspot.php --remove-mysql-sessions --force >/dev/null 2>&1 &");
}
function remove_mysql_sessions($aspid = false)
{
    $unix = new unix();
    $pidTime = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".time";
    if ($GLOBALS["VERBOSE"]) {
        $GLOBALS["FORCE"] = true;
        echo "pidTime: {$pidTime}\n";
        $GLOBALS["FORCE"] = true;
    }
    if (!$GLOBALS["FORCE"]) {
        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 (!$GLOBALS["FORCE"]) {
        $time = $unix->file_time_min($pidTime);
        if ($time < 10) {
            return;
        }
    }
    @file_put_contents($pidTime, time());
    $conntrack = $unix->find_program("conntrack");
    $q = new mysql_squid_builder();
    if ($q->COUNT_ROWS("hotspot_sessions") == 0) {
        remove_temp_sessions();
        untrack();
        $q->QUERY_SQL("DROP TABLE `hotspot_sessions`");
        $sql = "CREATE TABLE `squidlogs`.`hotspot_sessions` (\n\t\t\t`md5` VARCHAR( 90 ) NOT NULL ,\n\t\t\t`logintime` BIGINT UNSIGNED ,\n\t\t\t`maxtime` INT UNSIGNED ,\n\t\t\t`finaltime` INT UNSIGNED ,\n\t\t\t`nextcheck` INT UNSIGNED ,\n\t\t\t`username` VARCHAR( 128 ) NOT NULL ,\n\t\t\t`MAC` VARCHAR( 90 ) NOT NULL,\n\t\t\t`uid` VARCHAR( 128 ) NOT NULL ,\n\t\t\t`hostname` VARCHAR( 128 ) NOT NULL ,\n\t\t\t`ipaddr` VARCHAR( 128 ) ,\n\t\t\tPRIMARY KEY ( `md5` ) ,\n\t\t\tINDEX ( `logintime` , `maxtime` , `username` ,`finaltime`,`nextcheck`),\n\t\t\tKEY `MAC` (`MAC`),\n\t\t\tKEY `uid` (`uid`),\n\t\t\tKEY `hostname` (`hostname`),\n\t\t\tKEY `ipaddr` (`ipaddr`)\n\t\t\t)  ENGINE = MEMORY;";
        $q->QUERY_SQL($sql);
        return;
    }
    $iptables_save = $unix->find_program("iptables-save");
    shell_exec("{$iptables_save} > {$GLOBALS["CACHE_FILE"]}");
    $data = file_get_contents($GLOBALS["CACHE_FILE"]);
    $datas = explode("\n", $data);
    if ($GLOBALS["VERBOSE"]) {
        echo "DEBUG: {$GLOBALS["CACHE_FILE"]} -> " . count($datas) . " lines\n";
    }
    while (list($num, $ligne) = each($datas)) {
        if (!preg_match("#-m mac --mac-source (.+?)\\s+.*?--comment HotSpotSession-#", $ligne, $re)) {
            if ($GLOBALS["VERBOSE"]) {
                echo "DEBUG: {$ligne} (no match )\n";
            }
            continue;
        }
        $mac = trim(strtolower($re[1]));
        $ligne = mysql_fetch_array($q->QUERY_SQL("SELECT `md5` FROM hotspot_sessions WHERE MAC='{$mac}'"));
        if ($ligne["md5"] == null) {
            if ($GLOBALS["OUTPUT"]) {
                echo "Stopping......: " . date("H:i:s") . " [INIT]: {$GLOBALS["SERVICE_NAME"]} remove session \"{$mac}\"\n";
            }
            hotspot_admin_mysql(2, "Session {$mac} is not in table", "Remove it from firewall");
            remove_MAC_sessions($mac);
        }
    }
    $time = time();
    $sql = "SELECT `md5`,MAC,ipaddr,username,maxtime,nextcheck FROM hotspot_sessions WHERE nextcheck>0 AND nextcheck < {$time}";
    $results = $q->QUERY_SQL($sql);
    if (!$q->ok) {
        echo $q->mysql_error . "\n";
    }
    $count = mysql_num_rows($results);
    if ($count == 0) {
        if ($GLOBALS["VERBOSE"]) {
            echo "Nothing to do...\n";
        }
        return;
    }
    if ($GLOBALS["VERBOSE"]) {
        echo "{$count} rows\n";
    }
    while ($ligne = mysql_fetch_assoc($results)) {
        $MAC = $ligne["MAC"];
        $username = $ligne["username"];
        $ipaddr = $ligne["ipaddr"];
        $nextcheck = $ligne["nextcheck"];
        if ($GLOBALS["VERBOSE"]) {
            echo "\n\n****\n\n DROP Session {$MAC} {$username} {$ipaddr} {$nextcheck} < {$time}\n****\n\n";
        }
        remove_MAC_sessions($MAC);
        $NextCheckEdit = strtotime("+10 minutes", time());
        $sql = "UPDATE hotspot_sessions SET nextcheck={$NextCheckEdit} WHERE `md5`='{$ligne["md5"]}'";
        echo "{$sql}\n";
        $q->QUERY_SQL($sql);
        hotspot_admin_mysql(2, "Drop session {$username} {$ipaddr}", "Hotspot session for {$username} reach time [{$nextcheck}] " . date("Y-m-d H:i:s", $nextcheck) . " current " . date("Y-m-d H:i:s", $nextcheck) . "\nNext check will be at:" . date("Y-m-d H:i:s", $NextCheckEdit), __FILE__, __LINE__);
        shell_exec("{$conntrack} -D -n {$ipaddr} 2>&1");
    }
    $c = 0;
    $tab = array();
}
Example #3
0
function TESTCONNECTION($force = false)
{
    $sock = new sockets();
    $unix = new unix();
    if ($GLOBALS["FORCE"]) {
        $force = true;
    }
    $TimeFile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".time";
    $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid";
    if ($GLOBALS["VERBOSE"]) {
        echo "{$TimeFile}\n";
    }
    if (!$force) {
        $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());
    $TimeEx = $unix->file_time_min($TimeFile);
    if (!$force) {
        if ($TimeEx < 5) {
            die;
        }
        @unlink($TimeFile);
        @file_put_contents($TimeFile, time());
    }
    $ArticaHotSpotInterface = $sock->GET_INFO("ArticaHotSpotInterface");
    if ($ArticaHotSpotInterface == null) {
        $ArticaHotSpotInterface = "eth0";
    }
    $NETWORK_ALL_INTERFACES = $unix->NETWORK_ALL_INTERFACES();
    $IPADDR = $NETWORK_ALL_INTERFACES[$ArticaHotSpotInterface]["IPADDR"];
    $ArticaHotSpotPort = intval($sock->GET_INFO("ArticaHotSpotPort"));
    $uri = "http://{$IPADDR}:{$ArticaHotSpotPort}/wifidog/status";
    $curl = new ccurl($uri, true, $IPADDR, true);
    $curl->NoHTTP_POST = true;
    $curl->Timeout = 5;
    $curl->interface = $IPADDR;
    if (!$curl->get()) {
        if (!$GLOBALS["NO_RESTART"]) {
            hotspot_admin_mysql(0, "Checking HotSpot service failed [action=restart]", $curl->errors, __FILE__, __LINE__);
            $GLOBALS["RECOVER"] = true;
            stop(true);
            start(true);
        }
    }
    $f = explode("\n", $curl->data);
    while (list($num, $line) = each($f)) {
        $line = trim($line);
        if (preg_match("#IP:\\s+([0-9\\.]+)\\s+MAC:\\s+(.+)#", $line, $re)) {
            $MAC = trim(strtolower($re[2]));
            $IP = trim($re[1]);
            continue;
        }
        if (preg_match("#Token:\\s+(.+)#", $line, $re)) {
            $ARRAY["SESSIONS"][$MAC] = trim($re[1]);
        }
        if ($line == null) {
            continue;
        }
        if (preg_match("#Uptime:\\s+(.+)#", $line, $re)) {
            $ARRAY["UPTIME"] = trim($re[1]);
            continue;
        }
        if (preg_match("#Internet Connectivity:\\s+(.+)#", $line, $re)) {
            $ARRAY["INTERNET"] = trim($re[1]);
            continue;
        }
        if (preg_match("#Clients served this session:\\s+([0-9]+)#", $line, $re)) {
            $ARRAY["CLIENTS"] = trim($re[1]);
            continue;
        }
        if (preg_match("#([0-9]+)\\s+clients connected#", $line, $re)) {
            $ARRAY["CLIENTS"] = trim($re[1]);
            continue;
        }
        if ($GLOBALS["VERBOSE"]) {
            echo "No Match {$line}\n";
        }
    }
    $ARRAY["TIME"] = time();
    if ($GLOBALS["VERBOSE"]) {
        print_r($ARRAY);
    }
    @mkdir("/usr/share/artica/postfix/ressources/logs/web", 0755, true);
    file_put_contents("/usr/share/artica/postfix/ressources/logs/web/wifidog.status", serialize($ARRAY));
    chmod("/usr/share/artica/postfix/ressources/logs/web/wifidog.status", 0755);
    return $ARRAY;
}