CPClient.php 28.4 KB
Newer Older
Ad Schellevis's avatar
Ad Schellevis committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
<?php
/*
    # Copyright (C) 2014 Deciso B.V.
    #
    # All rights reserved.
    #
    # Redistribution and use in source and binary forms, with or without
    # modification, are permitted provided that the following conditions are met:
    #
    # 1. Redistributions of source code must retain the above copyright notice,
    #    this list of conditions and the following disclaimer.
    #
    # 2. Redistributions in binary form must reproduce the above copyright
    #    notice, this list of conditions and the following disclaimer in the
    #    documentation and/or other materials provided with the distribution.
    #—
    # THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
    # INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
    # AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
    # AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
    # OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    # POSSIBILITY OF SUCH DAMAGE.

    --------------------------------------------------------------------------------------
    package : Captive portal
    function: main package for captive portal backend functionality

*/


35 36 37
namespace OPNsense\CaptivePortal;

use \OPNsense\Core;
38 39 40 41

/**
 * Class CPClient
 * // TODO: CARP interfaces are probably not handled correctly
42
 * @package CaptivePortal
43 44 45 46 47 48 49 50 51 52 53 54
 */
class CPClient {


    /**
     * config handle
     * @var Core_Config
     */
    private  $config = null;

    /**
     * ipfw rule object
55
     * @var \CaptivePortal\Rules
56 57 58 59 60
     */
    private $rules = null;

    /**
     * link to shell object
Ad Schellevis's avatar
Ad Schellevis committed
61
     * @var  Core\Shell
62 63 64
     */
    private $shell = null;

65 66 67 68 69 70 71 72 73
    /**
     * send message to syslog
     *
     * @param $status
     * @param $user
     * @param $mac
     * @param $ip
     * @param string $message
     */
Ad Schellevis's avatar
Ad Schellevis committed
74
    private function logportalauth($cpzonename, $user, $mac, $ip, $status, $message=""){
75 76

        $message = trim($message);
Ad Schellevis's avatar
Ad Schellevis committed
77
        $message = "Zone : {$cpzonename} {$status}: {$user}, {$mac}, {$ip}, {$message}";
78 79 80 81 82 83 84 85

        $logger = new \Phalcon\Logger\Adapter\Syslog("logportalauth", array(
            'option' => LOG_PID,
            'facility' => LOG_LOCAL4
        ));
        $logger->info($message);

    }
Ad Schellevis's avatar
Ad Schellevis committed
86

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
    /**
     * Request new pipeno
     * @return int
     */
    private function new_ipfw_pipeno(){
        // TODO: implement global pipe number assigment
        return 999;
    }

    /**
     * reset bandwidth, if the current bandwidth is unchanged, do nothing
     *
     * @param int $pipeno system pipeno
     * @param int $bw bandwidth in Kbit/s
     */
    private function reset_bandwidth($pipeno,$bw){
        //TODO : setup bandwidth for sessions ( check changed )
        //#pipe 2000 config bw 2000Kbit/s
        return;
    }

    /**
     * @param $cpzonename zone name
     * @param $sessionid session id
     */
    private function _disconnect($cpzonename,$sessionid){

        $zoneid = -1;
        foreach($this->config->object()->captiveportal->children() as $zone => $zoneobj){
            if ($zone == $cpzonename) $zoneid = $zoneobj->zoneid;
        }

        if ($zoneid == -1) return; // not a valid zone

        $db = new DB($cpzonename);
        $db_clients = $db->listClients(array("sessionid"=>$sessionid));

        $ipfw_tables = $this->rules->getAuthUsersTables($zoneid);
        if ( sizeof($db_clients) > 0 ){
126
            if ($db_clients[0]->ip != null ) {
127 128 129 130 131 132 133 134 135 136 137
                // only handle disconnect if we can find a client in our database
                $exec_commands[] = "/sbin/ipfw table " . $ipfw_tables["in"] . " delete " . $db_clients[0]->ip;
                $exec_commands[] = "/sbin/ipfw table " . $ipfw_tables["out"] . " delete " . $db_clients[0]->ip;
                $this->shell->exec($exec_commands, false, false);
                // TODO: cleanup dummynet pipes $db_clients[0]->pipeno_in/out
                // TODO: log removal ( was : captiveportal_logportalauth($cpentry[4], $cpentry[3], $cpentry[2], "DISCONNECT");)
            }
            $db->remove_session($sessionid);
        }
    }

Ad Schellevis's avatar
Ad Schellevis committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
    /**
     * load accounting rules into ruleset, used for reinitialisation of the ruleset.
     * triggers add_accounting() for all active clients in all zones
     */
    private function loadAccounting()
    {
        foreach ($this->config->object()->captiveportal->children() as $cpzonename => $zone)
        {
            $db = new DB($cpzonename);
            foreach ($db->listClients(array()) as $client)
            {
                $this->add_accounting($zone->zoneid, $client->ip) ;
            }
            unset($db);
        }
    }

155 156 157 158 159 160
    /**
     *
     * @param $zoneid
     * @param $ip
     */
    public function add_accounting($zoneid,$ip){
Ad Schellevis's avatar
Ad Schellevis committed
161
        // TODO: check processing speed, this might need some improvement
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
        // check if our ip is already in the list and collect first free rule number to place it there if necessary
        $shell_output=array();
        $this->shell->exec("/sbin/ipfw show",false,false,$shell_output);
        $prev_id = 0;
        $new_id = null;
        foreach($shell_output as $line){
            // only trigger on counter rules and last item in the list
            if ( strpos($line," count " ) !== false || strpos($line,"65535 " )!== false  ) {
                if ( strpos($line," ".$ip." " ) !== false ) {
                    // already in table... exit
                    return;
                }

                $this_line_id = (int) (explode(" ",$line)[0]) ;
                if ( $this_line_id  > 30000 and ($this_line_id -1) > $prev_id and $new_id == null) {
                    // new id found
                    if ( $this_line_id == 65535 ) $new_id = $prev_id+1;
                    else $new_id = $this_line_id-1;
                }

                $prev_id =  $this_line_id;
            }
        }

        if ( $new_id != null ) {
            $exec_commands = array(
                "/sbin/ipfw add " . $new_id . " set " . $zoneid . " count ip from " . $ip . " to any ",
                "/sbin/ipfw add " . $new_id . " set " . $zoneid . " count ip from  any to " . $ip,
            );

            // execute all ipfw actions
            $this->shell->exec($exec_commands, false, false);
        }
    }

197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
    /**
     * list (ipfw) accounting information
     * @return array (key = hosts ip)
     */
    public function list_accounting($ipaddr=null){
        $filter_cmd = "";
        $result = array();
        $shell_output = array();
        if ( $ipaddr != null ) $filter_cmd =" | /usr/bin/grep ' " . $ipaddr ." '" ;

        if ( $this->shell->exec("/sbin/ipfw -aT list ".$filter_cmd,false,false,$shell_output) == 0 ){
            foreach( $shell_output as $line) {
                if (strpos($line, ' count ip from') !== false) {
                    $parts = preg_split('/\s+/', $line);
                    if (count($parts) > 8 && $parts[7] != 'any' and strlen($parts[7]) > 5) {
                        $result[$parts[7]] = array(
213
                            "rulenum" => $parts[0],
214 215 216 217 218 219 220 221 222 223 224 225 226 227
                            "last_accessed" => (int)$parts[3],
                            "idle_time" => time() - (int)$parts[3],
                            "out_packets" => (int)$parts[1],
                            "in_packets" => (int)$parts[2]
                        );
                    }
                }
            }
        }

        return $result;

    }

228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
    /**
     * reset traffic counters
     *
     * @param null $rulenum
     */
    public function zero_counters($rulenum=null){
        if ( $rulenum != null and is_numeric($rulenum) ){
            $this->shell->exec("/sbin/ipfw zero " . $rulenum );
        }
        elseif ( $rulenum == null ){
            $this->shell->exec("/sbin/ipfw zero " );
        }

    }

243 244 245 246 247
    /**
     * Constructor
     */
    function __construct() {
        // Request handle to configuration
Ad Schellevis's avatar
Ad Schellevis committed
248
        $this->config = Core\Config::getInstance();
249
        // generate new ruleset
250
        $this->rules = new Rules();
251
        // keep a link to the shell object
252
        $this->shell = new Core\Shell();
253 254 255 256 257 258 259 260 261 262 263 264 265
    }

    /**
     * Reconfigure zones ( generate and load ruleset )
     */
    public function reconfigure(){
        $ruleset_filename = \Phalcon\DI\FactoryDefault::getDefault()->get('config')->globals->temp_path."/ipfw.rules";
        $this->rules->generate($ruleset_filename);

        // load ruleset
        $this->shell->exec("/sbin/ipfw -f ".$ruleset_filename);

        // update tables
266
        $this->update();
Ad Schellevis's avatar
Ad Schellevis committed
267 268 269

        // after reinit all accounting rules are vanished, reapply them for active sessions
        $this->loadAccounting();
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
    }

    /**
     * update zone(s) with new configuration data
     * @param string $zone
     */
    public function update($zone=null){
        $this->refresh_allowed_ips($zone);
        $this->refresh_allowed_mac($zone);
    }

    /**
     * refresh allowed ip's for defined zone ( null for all zones )
     * @param string $zone
     */
    public function refresh_allowed_ips($cpzone=null){

        $handled_addresses = array();
        foreach( $this->config->object()->captiveportal->children()  as $cpzonename => $zone ) {
            // search requested zone (id)
            if ( $cpzonename == $cpzone || $zone->zoneid == $cpzone || $cpzone == null ) {
                $db = new DB($cpzonename);
                $db_iplist = $db->listFixedIPs();

                // calculate table numbers for this zone
                $ipfw_tables = $this->rules->getAuthIPTables($zone->zoneid );

                foreach ($zone->children() as $tagname => $tagcontent) {
                    $ip = $tagcontent->ip->__toString();
                    if ($tagname == 'allowedip') {
                        $handled_addresses[$ip] = array();
                        $handled_addresses[$ip]["bw_up"] = $tagcontent->bw_up->__toString() ;
                        $handled_addresses[$ip]["bw_down"] = $tagcontent->bw_down->__toString() ;

                        if ( !array_key_exists($ip,$db_iplist) ){
                            // only insert new values
                            $pipeno_in = $this->new_ipfw_pipeno() ;
                            $pipeno_out = $this->new_ipfw_pipeno() ;

                            $exec_commands = array(
                                # insert new ip address
                                "/sbin/ipfw table ". $ipfw_tables["in"]  ." add " . $ip . "/" . $tagcontent->sn->__toString() . " " . $pipeno_in,
                                "/sbin/ipfw table ". $ipfw_tables["out"] ." add " . $ip . "/" . $tagcontent->sn->__toString() . " " . $pipeno_out,
                            );

                            // execute all ipfw actions
                            $this->shell->exec($exec_commands, false,false);
                            // update administration
                            $db->upsertFixedIP($ip,$pipeno_in,$pipeno_out);
                            // save bandwidth data
                            $handled_addresses[$ip]["pipeno_in"] = $pipeno_in ;
                            $handled_addresses[$ip]["pipeno_out"] =  $pipeno_out ;
                        }else{
                            //
                            $handled_addresses[$ip]["pipeno_in"] = $db_iplist[$ip]->pipeno_in ;
                            $handled_addresses[$ip]["pipeno_out"] = $db_iplist[$ip]->pipeno_out ;
                        }
                    }

                }


                // Cleanup deleted addresses
                foreach($db_iplist as $ip => $record){
                    if (!array_key_exists($ip,$handled_addresses)){
                        $exec_commands = array(
                            # insert new ip address
                            "/sbin/ipfw table ". $ipfw_tables["in"]  ." del " . $ip . "/" . $tagcontent->sn->__toString() ,
                            "/sbin/ipfw table ". $ipfw_tables["out"] ." del " . $ip . "/" . $tagcontent->sn->__toString() ,
                        );

                        // execute all ipfw actions
                        $this->shell->exec($exec_commands, false,false);
                        // TODO : cleanup $record->pipeno_in, $record->pipeno_out ;
                        $db->dropFixedIP($ip);
                    }
                }

                // reset bandwidth,
                foreach($handled_addresses as $mac => $record){
                    if (array_key_exists("pipeno_in",$record) ){
                        $this->reset_bandwidth($record["pipeno_in"],$record["bw_down"]);
                        $this->reset_bandwidth($record["pipeno_out"],$record["bw_up"]);
                    }
                }

                unset($db);
            }
        }

    }

    /**
     * To be able to grant access to physical pc's, we need to do some administration.
     * Our captive portal database keeps a list of every used address and last know mac address
     *
     * @param String $zone zone name or number
     */
    public function refresh_allowed_mac($cpzone=null){

        // read ARP table
        $arp= new ARP();
        $arp_maclist = $arp->getMACs();

        // keep a list of handled addresses, so we can cleanup the rest and keep track of needed bandwidth restrictions
        $handled_mac_addresses = array();
        foreach( $this->config->object()->captiveportal->children()  as $cpzonename => $zone ) {
            if ( $cpzonename == $cpzone || $zone->zoneid == $cpzone || $cpzone == null ) {
                // open administrative database for this zone
                $db = new DB($cpzonename);
                $db_maclist = $db->listPassthruMacs();
                $ipfw_tables = $this->rules->getAuthMACTables($zone->zoneid);

                foreach ($zone->children() as $tagname => $tagcontent) {
                    $mac = trim(strtolower($tagcontent->mac));
                    if ($tagname == 'passthrumac') {
                        // only accept valid macaddresses
                        if (preg_match('/^([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}$/', $mac)) {
                            if ($tagcontent->action == "pass") {
                                $handled_mac_addresses[$mac] = array("action"=>"skipped" );
                                $handled_mac_addresses[$mac]["bw_up"] = $tagcontent->bw_up ;
                                $handled_mac_addresses[$mac]["bw_down"] = $tagcontent->bw_down ;

                                // only handle addresses we know of
                                if (array_key_exists($mac, $arp_maclist)) {
                                    // if the address is already in our database, check if it has changed
                                    if ( array_key_exists($mac,$db_maclist) ) {
                                        // save pipe numbers for bandwidth restriction
                                        $handled_mac_addresses[$mac]["pipeno_in"] = $db_maclist[$mac]->pipeno_in ;
                                        $handled_mac_addresses[$mac]["pipeno_out"] = $db_maclist[$mac]->pipeno_out ;

                                        if ($db_maclist[$mac]->ip !=  $arp_maclist[$mac]['ip'] ) {
                                            // handle changed ip,
                                            $handled_mac_addresses[$mac]["action"] = "changed ip";
                                            $exec_commands = array(
                                                # delete old ip address
                                                "/sbin/ipfw table ". $ipfw_tables["in"] ." delete ". $db_maclist[$mac]->ip,
                                                "/sbin/ipfw table ". $ipfw_tables["out"] ." delete ". $db_maclist[$mac]->ip,
                                                # insert new ip address
                                                "/sbin/ipfw table ". $ipfw_tables["in"]  ." add " . $arp_maclist[$mac]['ip']. " " . $db_maclist[$mac]->pipeno_in,
                                                "/sbin/ipfw table ". $ipfw_tables["out"] ." add " . $arp_maclist[$mac]['ip']. " " . $db_maclist[$mac]->pipeno_out,
                                            );

                                            // execute all ipfw actions
                                            $this->shell->exec($exec_commands, false,false);
                                            // update administration
                                            $db->upsertPassthruMAC($tagcontent->mac,$arp_maclist[$mac]['ip'],$db_maclist[$mac]->pipeno_in,$db_maclist[$mac]->pipeno_out); // new ip according to arp table
                                        }
                                    }
                                    else {
                                        // new host, not seen it yet
                                        $handled_mac_addresses[$mac]["action"] = "new";
                                        $pipeno_in = $this->new_ipfw_pipeno() ;
                                        $pipeno_out = $this->new_ipfw_pipeno() ;

                                        // execute all ipfw actions
                                        $exec_commands = array(
                                            # insert new ip address
                                            "/sbin/ipfw table ". $ipfw_tables["in"]  ." add " . $arp_maclist[$mac]['ip']. " " . $pipeno_in,
                                            "/sbin/ipfw table ". $ipfw_tables["out"] ." add " . $arp_maclist[$mac]['ip']. " " . $pipeno_out,
                                        );
                                        $this->shell->exec($exec_commands, false,false);

                                        $db->upsertPassthruMAC($tagcontent->mac,$arp_maclist[$mac]['ip'],$pipeno_in,$pipeno_out);
                                        // save pipe numbers for bandwidth restriction
                                        $handled_mac_addresses[$mac]["pipeno_in"] = $pipeno_in ;
                                        $handled_mac_addresses[$mac]["pipeno_out"] =  $pipeno_out ;
                                    }
                                }
                            }
                        }
                    }
                }

                //
                // cleanup old addresses
                //
                foreach($db_maclist as $mac => $record){
                    if ( !array_key_exists($mac,$handled_mac_addresses) ){
                        # delete old ip address, execute all actions
                        $exec_commands = array(
                            "/sbin/ipfw table ". $ipfw_tables["in"] ." delete ". $db_maclist[$mac]->ip,
                            "/sbin/ipfw table ". $ipfw_tables["out"] ." delete ". $db_maclist[$mac]->ip,
                        );
                        $this->shell->exec($exec_commands, false,false);
                        // TODO : cleanup $record->pipeno_in, $record->pipeno_out ;
                        $db->dropPassthruMAC($mac);
                    }
                }

                // reset bandwidth
                foreach($handled_mac_addresses as $mac => $record){
                    if (array_key_exists("pipeno_in",$record) ){
                        $this->reset_bandwidth($record["pipeno_in"],$record["bw_down"]);
                        $this->reset_bandwidth($record["pipeno_out"],$record["bw_up"]);
                    }
                }

                unset($db);

            }
        }

    }

    /**
476 477
     * unlock host for captiveportal use
     *
478 479 480 481 482 483 484 485
     * @param string $cpzonename
     * @param string $clientip
     * @param string $clientmac
     * @param string $username
     * @param string $password
     * @param string $attributes
     * @param string $radiusctx
     */
486
    public function portal_allow($cpzonename,$clientip,$clientmac,$username,$password = null,$bw_up=null,$bw_down=null, $radiusctx = null,$session_timeout=null,$idle_timeout=null,$session_terminate_time=null,$interim_interval=null){
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
        // defines
        $exec_commands = array() ;
        $db = new DB($cpzonename);
        $arp= new ARP();

        // find zoneid for this named zone
        $zoneid = -1;
        foreach($this->config->object()->captiveportal->children() as $zone => $zoneobj){
            if ($zone == $cpzonename) $zoneid = $zoneobj->zoneid;
        }

        if ($zoneid == -1) return; // not a valid zone, bailout


        // grap needed data to generate our rules
        $ipfw_tables = $this->rules->getAuthUsersTables($zoneid);
        $cp_table = $db->listClients(array("mac"=>$clientmac,"ip"=>$clientip),"or");
        if ( sizeof($cp_table) > 0 && ($cp_table[0]->ip == $clientip && $cp_table[0]->mac == $clientmac ) ){
            // nothing (important) changed here... move on
506
            return $cp_table[0]->sessionid;
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
        } elseif ( sizeof($cp_table) > 0) {
            // something changed...
            // prevent additional sessions to popup, one MAC should have only one active session, remove the rest (if any)
            $cnt = 0;
            $remove_sessions = array();
            foreach($cp_table as $record){
                if ( $cnt >0) $remove_sessions[] = $record->sessionid;
                else $current_session = $record;
                $cnt++;
                // prepare removal for all ip addresses belonging to this host
                $exec_commands[] = "/sbin/ipfw table ". $ipfw_tables["in"] ." delete ". $record->ip;
                $exec_commands[] = "/sbin/ipfw table ". $ipfw_tables["out"] ." delete ". $record->ip;
                // TODO: if for some strange reason there is more than one session, we are failing to drop the pipes
                $exec_commands[] = "/usr/sbin/arp -d ".trim($record->ip); // drop static arp entry (prevent MAC change)
            }
            if (sizeof($remove_sessions)){
                $db->remove_session($remove_sessions);
            }

            // collect pipe numbers for  dummynet
            $pipeno_in = $current_session->pipeno_in;
            $pipeno_out = $current_session->pipeno_out;

            $db->update_session($current_session->sessionid,array("ip"=>$clientip,"mac"=>$clientmac));
531 532 533

            // preserve session for response
            $sessionid = $current_session->sessionid;
534 535 536 537 538 539 540 541 542 543 544 545 546 547
        } else
        {
            // new session, allocate new dummynet pipes and generate a unique id
            $pipeno_in = $this->new_ipfw_pipeno();
            $pipeno_out = $this->new_ipfw_pipeno();

            // construct session data
            $session_data=Array();
            $session_data["ip"]=$clientip;
            $session_data["mac"]=$clientmac;
            $session_data["pipeno_in"] = $pipeno_in;
            $session_data["pipeno_out"] = $pipeno_out;
            $session_data["username"]=\SQLite3::escapeString($username);
            $session_data["bpassword"] =base64_encode($password);
548 549 550 551
            $session_data["session_timeout"] = $session_timeout;
            $session_data["idle_timeout"] = $idle_timeout;
            $session_data["session_terminate_time"] = $session_terminate_time;
            $session_data["interim_interval"] = $interim_interval;
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
            $session_data["radiusctx"] = $radiusctx;
            $session_data["allow_time"] = time(); // allow time is actual starting time of this session
            $sessionid = uniqid() ;

            $db->insert_session($sessionid, $session_data );

        }

        // add commands for access tables, and execute all collected
        $exec_commands[] = "/sbin/ipfw table ". $ipfw_tables["in"] ." add ". $clientip . " ".$pipeno_in;
        $exec_commands[] = "/sbin/ipfw table ". $ipfw_tables["out"] ." add ". $clientip . " ".$pipeno_out;
        $this->shell->exec($exec_commands, false,false);

        // lock the user/ip to it's MAC address using arp
        $arp->setStatic($clientip,$clientmac);

        // add accounting rule
        $this->add_accounting($zoneid,$clientip);

571 572 573 574
        // set bandwidth restrictions
        $this->reset_bandwidth($pipeno_in,$bw_up);
        $this->reset_bandwidth($pipeno_in,$bw_down);

575
        // log
Ad Schellevis's avatar
Ad Schellevis committed
576
        $this->logportalauth($cpzonename, $username, $clientmac, $clientip, $status="LOGIN");
577

578 579
        // cleanup
        unset($db);
580 581

        return $sessionid;
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
    }

    /**
     * disconnect a session or a list of sessions depending on the parameter
     * @param string $cpzonename zone name or id
     * @param $sessionid
     */
    public function disconnect($cpzonename,$sessionid){
        if ( is_array($sessionid)){
            foreach($sessionid as $sessid ){
                $this->_disconnect($cpzonename,$sessid);
            }
        }
        else{
            $this->_disconnect($cpzonename,$sessionid);
        }
    }

    /**
     * flush zone (null flushes all zones)
     * @param null $zone
     */
    function flush($zone=null){
        if ( $zone == null ) {
606
            $shell = new Core\Shell();
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
            $shell->exec("/sbin/ipfw -f table all flush");
        }
        else{
            // find zoneid for this named zone
            if (preg_match("/^[0-9]{1,2}$/", trim($zone)) ) {
                $zoneid = $zone;
            }else {
                $zoneid = -1;
                foreach ($this->config->object()->captiveportal->children() as $zonenm => $zoneobj) {
                    if ($zonenm == $zone) $zoneid = $zoneobj->zoneid;
                }
            }

            if ( $zoneid != -1 ){
                $exec_commands= array(
                    "/sbin/ipfw -f table ".$this->rules->getAuthUsersTables($zoneid)["in"]." flush",
                    "/sbin/ipfw -f table ".$this->rules->getAuthUsersTables($zoneid)["out"]." flush",
                    "/sbin/ipfw -f table ".$this->rules->getAuthIPTables($zoneid)["in"]." flush",
                    "/sbin/ipfw -f table ".$this->rules->getAuthIPTables($zoneid)["out"]." flush",
                    "/sbin/ipfw -f table ".$this->rules->getAuthMACTables($zoneid)["in"]." flush",
                    "/sbin/ipfw -f table ".$this->rules->getAuthMACTables($zoneid)["out"]." flush",
                    "/sbin/ipfw delete set ".$zoneid,
                );
                $this->shell->exec($exec_commands, false,false);
            }
        }
    }

635 636 637
    /**
     * cleanup portal sessions
     */
638
    function portal_cleanup_sessions($cpzone=null){
639 640
        $acc_list = $this->list_accounting();
        foreach($this->config->object()->captiveportal->children() as $cpzonename => $zoneobj){
641 642
            if ( $cpzone == null || $cpzone == $cpzonename ) {
                $db = new DB($cpzonename);
643

644
                $clients = $db->listClients(array(), null, null);
645

646 647 648 649 650
                foreach ($clients as $client) {
                    $idle_time = 0;
                    if (array_key_exists($client->ip, $acc_list)) {
                        $idle_time = $acc_list[$client->ip];
                    }
651

652 653 654 655
                    // if session timeout is reached, disconnect
                    if (is_numeric($client->session_timeout) && $client->session_timeout > 0 ) {
                        if (((time() - $client->allow_time) / 60) > $client->session_timeout) {
                            $this->disconnect($cpzonename, $client->sessionid);
Ad Schellevis's avatar
Ad Schellevis committed
656
                            $this->logportalauth($cpzonename, $client->username, $client->mac, $client->ip, $status="SESSION TIMEOUT");
657 658 659 660 661 662 663 664
                            continue;
                        }
                    }

                    // disconnect session if idle timeout is reached
                    if (is_numeric($client->idle_timeout) && $client->idle_timeout > 0  && $idle_time > 0) {
                        if ($idle_time > $client->idle_timeout) {
                            $this->disconnect($cpzonename, $client->sessionid);
Ad Schellevis's avatar
Ad Schellevis committed
665
                            $this->logportalauth($cpzonename, $client->username, $client->mac, $client->ip, $status="IDLE TIMEOUT");
666 667
                            continue;
                        }
668 669
                    }

670
                    // disconnect on session terminate time
671
                    if ( is_numeric($client->session_terminate_time) && $client->session_terminate_time > 0 && $client->session_terminate_time < time()) {
672
                        $this->disconnect($cpzonename, $client->sessionid);
Ad Schellevis's avatar
Ad Schellevis committed
673
                        $this->logportalauth($cpzonename, $client->username, $client->mac, $client->ip, $status="TERMINATE TIME REACHED");
674 675 676 677
                        continue;
                    }
                }

678 679
                unset($db);
            }
680 681 682 683 684
        }

        unset ($acc_list);

    }
685 686 687


}