FreeBSD 10.1 x64 WiFi Captive Portal

Wifi-captive-portal

Our goal is to build a FreeBSD server Captive Portal as it is in hotels and airports. Guest joins to wifi without entering a username or password, but when trying to join internet the user will be asked for username and password. If the username and password is true, the guest will be able to use internet. In my environment I used AP ubiquiti(official site: https://www.ubnt.com/). It is cheaper, stable and have open source wireless controller. The network structure will be as follows:
UNIXMEN-FR

It is assumed that you have already configured FAMP and Apache PHP is stable. Apache web server is working on a group and user named unixmen which is created before. (In other words, in httpd.conf file directives are available: User unixmen and Group unixmen)

In all Access Points IP addresses is configured as it is shown in picture and DHCP server and ROUTER, FreeBSD Server’s internal network card’s IP address is shown. In Apache configured VirtualHost and it operates under the domain name wifi.unixmen.com

VirtualHost’s Public_html folder is the address of /usr/local/www/wifi/, and the  membership and authority of folder is unixmen. And same as /usr/local/etc/apache24/httpd.conf configured file Listen 80 and Listen 443 defined. /usr/local/domen/wifi.unixmen.com virtualhos content of the file are as follows:

<strong>&lt;VirtualHost *:80&gt;</strong>
<strong>        RewriteEngine on</strong>
<strong>        ReWriteCond %{SERVER_PORT} !^443$</strong>
<strong>        RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R,L]</strong>
<strong>&lt;/VirtualHost&gt;</strong>
<strong>&lt;VirtualHost *:443&gt;</strong>
<strong>        SSLEngine on</strong>
<strong>        SSLCertificateFile /usr/local/etc/apache24/ssl/wifi.pem</strong>
<strong>        SSLCertificateKeyFile /usr/local/etc/apache24/ssl/wifi.key</strong>
<strong>        DocumentRoot /usr/local/www/wifi/</strong>
<strong>&lt;Directory "/usr/local/www/wifi"&gt;</strong>
<strong>        AllowOverride All</strong>
<strong>        Require all granted</strong>
<strong>&lt;/Directory&gt;</strong>
<strong>&lt;/VirtualHost&gt;</strong>

It is needed to install pear for php and create database and its user at MySQL We install pear and configure for php.

# <strong>cd `whereis pear | awk '{ print $2 }'`</strong>  - enter Port Path
# <strong>make -DBATCH install</strong>                    - install

After we copy php file and change properly the changes of the context.

# <strong>cp /usr/local/etc/php.ini-production /usr/local/etc/php.ini</strong>

We do the changes in the file /usr/local/etc/php.ini:

<strong>date.timezone = 'Asia/Baku'</strong>
<strong>include_path = '.:/usr/local/share/pear'</strong>

Now lets create MySQL database and define a user for this database. And create a schedule for wifi users

mysql&gt; <strong>CREATE database wifi;</strong>
mysql&gt; <strong>grant all privileges on wifi.* to wifidbuser@localhost identified by 'wifidbpassword';</strong>
mysql&gt; <strong>use</strong> <strong>wifi</strong>;
mysql&gt; <strong>CREATE TABLE `users` (</strong>
<strong>  `id` int(10) unsigned NOT NULL auto_increment,</strong>
<strong>  `username` varchar(50) default NULL,</strong>
<strong>  `password` varchar(50) default NULL,</strong>
<strong>  `created` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,</strong>
<strong>  `time_begin` timestamp NOT NULL default '0000-00-00 00:00:00',</strong>
<strong>  `time_end` timestamp NOT NULL default '0000-00-00 00:00:00',</strong>
<strong>  `status` tinyint(4) NOT NULL default '0',</strong>
<strong>  `rule_num` smallint(5) unsigned NOT NULL,</strong>
<strong>  PRIMARY KEY  (`id`),</strong>
<strong>  KEY `rule_num` (`rule_num`)</strong>
<strong>) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=cp1251 AUTO_INCREMENT=6 ;</strong>

/etc/rc.conf the configuration file will be as follows

<strong>hostname="wifinat.unixmen.com"</strong>
<strong>ifconfig_em0="inet 88.8.9.10 netmask 255.255.255.0"</strong>
<strong>ifconfig_em1="inet 10.0.0.1 netmask 255.255.255.0"</strong>
<strong>defaultrouter="88.8.9.1"</strong>
<strong>sshd_enable="YES"</strong>
<strong>dumpdev="NO"</strong>

#### Local Disabled Services ####
sendmail_enable=”NO”
sendmail_submit_enable=”NO”
sendmail_outbound_enable=”NO”
sendmail_msp_queue_enable=”NO”
sendmail_rebuild_aliases=”NO”
syslogd_enable=”YES”
syslogd_program=”/usr/sbin/syslogd”
syslogd_flags=”-ss”

#### Local worked services ####
tcp_drop_synfin=”YES”
icmp_drop_redirects=”YES”
dhcpd_enable=”YES”
dhcpd_ifaces=”em1″
dhcpd_conf=”/usr/local/etc/dhcpd.conf”
gateway_enable=”YES”
natd_enable=”YES”
natd_interface=”em0″
firewall_enable=”YES”
firewall_type=”UNKNOWN”
firewall_script=”/etc/ipfw.conf”

#### Third Party Services ####
apache24_enable=”YES”
mysql_enable=”YES”

we add the following lines to the file /etc/ipfw.conf in order our configured firewall to run after reboot.

<strong>ipfw add 00200 divert 8668 ip from any to any via em0</strong>
<strong>ipfw add 10800 allow ip from any to 88.8.9.58</strong>
<strong>ipfw add 10900 allow ip from 88.8.9.58 to any</strong>
<strong>ipfw add 11000 allow ip from any to 88.8.9.59</strong>
<strong>ipfw add 12000 allow ip from 88.8.9.59 to any</strong>
<strong>ipfw add 60000 fwd 10.0.0.1,80 tcp from any to any dst-port 80 via em1</strong>
<strong>ipfw add 60001 fwd 10.0.0.1,443 tcp from any to any dst-port 443 via em1</strong>
<strong>ipfw add 65000 allow ip from any to any</strong>
<strong>ipfw add 65535 deny ip from any to any</strong>

Minimize system kernel and compile the following options:

<strong>options         IPDIVERT</strong>
<strong>options         IPFIREWALL</strong>
<strong>options         IPFIREWALL_VERBOSE</strong>
<strong>options         IPFIREWALL_VERBOSE_LIMIT=3</strong>
<strong>options         DUMMYNET</strong>
<strong>options         IPFIREWALL_FORWARD</strong>
<strong>options         IPFIREWALL_NAT</strong>
<strong>options         LIBALIAS</strong>

Install DHCP server from ports

# <strong>cd /usr/ports/net/isc-dhcp42-server/</strong>    - enter the port address
# <strong>make config</strong>                             - Choose the required modules
<a href="http://unixmen.com/wp-content/uploads/2016/01/dhcp-port.png" rel="attachment wp-att-43181"><img class="aligncenter size-full wp-image-43181" src="http://unixmen.com/wp-content/uploads/2016/01/dhcp-port.png" alt="dhcp-port" width="535" height="184" /></a>
# <strong>make -DBATCH install</strong>        - install

/usr/local/etc/dhcpd.conf we make installation file content as following:

<strong>option domain-name "wifiofis.unixmen.com";</strong>
<strong>option domain-name-servers ns1.unixmen.com, ns2.unixmen.com;</strong>
<strong>default-lease-time 3600;</strong>
<strong>max-lease-time 86400;</strong>
<strong>ddns-update-style none;</strong>
<strong>authoritative;</strong>
<strong>subnet 10.0.0.0 netmask 255.255.255.0 {</strong>
<strong>        range 10.0.0.50 10.0.0.254;</strong>
<strong>        option routers 10.0.0.1;</strong>
<strong>}</strong>
# we reserve Access Points for the IP addresses shown in the picture
<strong>host Wifi-1f.1 {</strong>
<strong>        hardware ethernet 04:18:76:8c:9a:a3;</strong>
<strong>        fixed-address 10.0.0.10;</strong>
<strong>}</strong>
<strong>host Wifi-2f.1 {</strong>
<strong>        hardware ethernet 04:18:66:43:11:11;</strong>
<strong>        fixed-address 10.0.0.20;</strong>
<strong>}</strong>
<strong>host Wifi-3f.1 {</strong>
<strong>        hardware ethernet 04:18:36:68:a9:9b;</strong>
<strong>        fixed-address 10.0.0.30;</strong>

we create a journal file for DHCP and filter from Syslog(/var/db/dhcpd/dhcpd.leases (you can look in this file)

# <strong>touch /var/log/dhcp.log</strong>

We add the follofing lines to the end of /etc/syslog.conf file:

<strong>!dhcpd</strong>
<strong>*.*                /var/log/dhcp.log</strong>

We run the DHCP and check listening:

# <strong>/usr/local/etc/rc.d/isc-dhcpd start</strong>
# <strong>sockstat -l|grep dhcp</strong>
dhcpd    dhcpd      4695  7  udp4   *:67                  *:*
dhcpd    dhcpd      4695  20 udp4   *:8997                *:*

Install Sudo from the ports:

# <strong>cd `whereis sudo | awk '{ print $2 }'`</strong>  - change directory to the port
# <strong>make config</strong>                             - choose required modules
<a href="http://unixmen.com/wp-content/uploads/2016/01/sudo-port.png" rel="attachment wp-att-43189"><img class="aligncenter size-full wp-image-43189" src="http://unixmen.com/wp-content/uploads/2016/01/sudo-port.png" alt="sudo-port" width="654" height="270" /></a>
# <strong>make -DBATCH install</strong>                    - install

Add to /usr/local/etc/sudoers file the following lines(it is required for access of web server to firewall):

<strong>unixmen ALL=(ALL) NOPASSWD: SETENV: ALL</strong>

/usr/local/www/wifi/config.php the content of configuration file will be as following lines(the registration and stop registered users is fulfill by adding or removing the rules of IPFW):

# <strong>cat /usr/local/www/wifi/config.php</strong>
<strong>&lt;?php</strong>
<strong>define('DEBUG', true);</strong>
<strong>define('conf_DB_HOST', 'localhost');</strong>            //Database IP
<strong>define('conf_DB_USER', 'wifidbuser');</strong>           //username of database
<strong>define('conf_DB_PASS', 'wifidbpassword');</strong>       //the password of database
<strong>define('conf_DB_NAME', 'wifi');</strong>                 //the name of database
<strong>define('RULE_NUM_MIN', 400);                    </strong>//IPFW rules begin from 400
<strong>define('RULE_NUM_MAX', 600);                    </strong>//IPFW rule finishs with 600
<strong>define('CLIENTS_IP_BEGIN', '10.0.0.50');</strong> // the clients will start IP adress from
<strong>define('CLIENTS_IP_COUNT', '200');</strong>
<strong>define('CLIENTS_TIME', '3600');</strong> //the period of internet access for users(an hour)
<strong>define('RULE_ADD_IP', 'sudo ipfw add %s allow ip from any to %s');</strong>
<strong>define('RULE_ADD_IP2', 'sudo ipfw add %s allow ip from %s to any');</strong>
<strong>define('RULE_DEL_IP', 'sudo ipfw del %s');</strong>
<strong>define('RULE_DEL_IP2', 'sudo ipfw del %s');</strong>
<strong>/*</strong>
STATUS:
<strong>0</strong> – If the information of connection is true you are connected, otherwise the rule is not added!
<strong>1</strong> -  You have already connected
<strong>2</strong> – The username has been already used.
<strong>3</strong> – The user is stopped.
<strong>*/</strong>
<strong>$db_link = mysql_connect(conf_DB_HOST, conf_DB_USER, conf_DB_PASS);</strong>
<strong>if (!$db_link) return cms_errors('The connection to database is failed!);</strong>
<strong>if (!mysql_select_db(conf_DB_NAME, $db_link)) return cms_errors('The connection to database is failed!!!');</strong>
<strong>function cms_errors($text)</strong>
<strong>{</strong>
<strong>        if (DEBUG) echo $text;</strong>
<strong>        return false;</strong>
<strong>}</strong>
<strong>function dumpVarX(&amp;$Var, $Var_s = null)</strong>
<strong>{</strong>
<strong>        echo "&lt;div align='left' class='debug'&gt;";</strong>
<strong>        dumpVar($Var, 0, $Var_s);</strong>
<strong>        echo "&lt;div&gt;";</strong>
<strong>        return true;</strong>
<strong>}</strong>
<strong>function dumpVar(&amp;$Var, $Level = 0, $Var_s = null)</strong>
<strong>{</strong>
<strong>        if ($Level &gt; 4)</strong>
<strong>        {</strong>
<strong>                echo "&lt;b&gt;...&lt;/b&gt; LEVEL &gt; 4&lt;br&gt;\n";</strong>
<strong>                return;</strong>
<strong>        }</strong>
<strong>        $is_ob_ar = false;</strong>
<strong>        $Type = gettype($Var);</strong>
<strong>        if (is_array($Var)) {$is_ob_ar = true; $Type = "Array[".count($Var)."]";}</strong>
<strong>        if (is_object($Var)) $is_ob_ar = true;</strong>
<strong>        if ($Level == 0)</strong>
<strong>        {</strong>
<strong>                if ($Var_s) echo "\n&lt;br&gt;\n&lt;b&gt;&lt;span style="color:#ff0000"&gt;".$Var_s." = {&lt;/span&gt;&lt;/b&gt;";</strong>
<strong>                if ($is_ob_ar &amp;&amp; count($Var)) echo "&lt;pre&gt;\n";</strong>
<strong>                else echo "\n&lt;tt&gt;";</strong>
<strong>                $Level_zero = 0;</strong>
<strong>        }</strong>
<strong>        if ($is_ob_ar)</strong>
<strong>        {</strong>
<strong>                echo "&lt;span style="color:#05a209"&gt;$Type&lt;/span&gt;\n";</strong>
<strong>                for (Reset($Var), $Level++; list($k, $v)=each($Var);)</strong>
<strong>                {</strong>
<strong>                        if (is_array($v) &amp;&amp; $k==="GLOBALS") continue;</strong>
<strong>                        for ($i=0; $i&lt;$Level*3; $i++) echo " ";</strong>
<strong>                        echo "&lt;b&gt;".HtmlSpecialChars($k)."&lt;/b&gt; =&gt; ";</strong>
<strong>                        dumpVar($v, $Level);</strong>
<strong>                }</strong>
<strong>        }</strong>
<strong>        else</strong>
<strong>        {</strong>
<strong>                if (is_string($Var) &amp;&amp; strlen($Var)&gt;400)</strong>
<strong>                        echo '('.$Type.') &lt;span style="color:#35BBFA"&gt;strlen = '.strlen($Var).'&lt;/span&gt;'."\n";</strong>
<strong>                else echo '('.$Type.') "&lt;span style="color:#0000FF"&gt;',HtmlSpecialChars($Var),'&lt;/span&gt;"'."\n";</strong>
<strong>        }</strong>
<strong>        if (isset($Level_zero))</strong>
<strong>        {</strong>
<strong>                if ($is_ob_ar &amp;&amp; count($Var)) echo "&lt;/pre&gt;";</strong>
<strong>                else echo "&lt;/tt&gt;";</strong>
<strong>                if ($Var_s) echo "&lt;b&gt;&lt;span style="color:#ff0000"&gt;}&lt;/span&gt;&lt;/b&gt;&lt;br&gt;\n";</strong>
<strong>        }</strong>
<strong>        return true;</strong>
<strong>}</strong>
<strong>?&gt;</strong>

User’s registration script: /usr/local/www/wifi/add.php file will be as follows:

# <strong>cat /usr/local/www/wifi/add.php</strong>
<strong>&lt;?php</strong>
<strong>require_once('config.php');</strong>
<strong>$user = get_user($_GET['login'], $_GET['pass']);</strong>
<strong>if ($user)</strong>
<strong>{</strong>
<strong>        switch ($user['status'])</strong>
<strong>        {</strong>
<strong>                case 0:</strong>
<strong>                        if (add_rule($user)) echo '&lt;h2&gt;You are connected!&lt;/h2&gt;';</strong>
<strong>                                else echo 'The wrong rule is not added!';</strong>
<strong>                        break;</strong>
<strong>                case 1: echo '&lt;h2&gt;You have already connected&lt;/h2&gt;'; break;</strong>
<strong>                case 2: echo '&lt;h2&gt;Username has already been used&lt;/h2&gt;'; break;</strong>
<strong>                case 2: echo '&lt;h2&gt;Username is stopped&lt;/h2&gt;'; break;</strong>
<strong>                default: echo 'Error'; break;</strong>
<strong>        }</strong>
<strong>} else echo '&lt;h2&gt;Username or password is wrong&lt;/h2&gt;';</strong>

// Registration
function get_user($login, $pass)
{
        $user = null;
        if (!$login || !$pass) return null;
        $login = addslashes($login);
        $sql = ‘SELECT * FROM users WHERE username=”‘.$login.'” AND password=”‘.$pass.'” LIMIT 1’;
        $res = mysql_query($sql);
        if ($res) $user = mysql_fetch_assoc($res);
        return $user;
}

// Adding the rule
function add_rule($user)
{
        $user_ip = $_SERVER[‘REMOTE_ADDR’];
        $current_date = time();
        if (!checkip($user_ip)) return false;
        $temp = 0;
        $sql = ‘SELECT rule_num FROM users WHERE status=1 ORDER BY rule_num’;
        $res = mysql_query($sql);
        if ($res)
        {
                $t = mysql_fetch_array($res);
                if (!$t) $rule_num = RULE_NUM_MIN;
                else {
                        while ($temp = mysql_fetch_array($res))
                        {
                                if (($t[0]+1) < $temp[0]) break;
                                $t = $temp;
                        }
                        if ($t[0] < RULE_NUM_MAX) $rule_num = $t[0]+1; else return false;
                }
        } else return false;
        $command = sprintf(RULE_ADD_IP, $rule_num, $user_ip);
        exec($command);
        $command2 = sprintf(RULE_ADD_IP2, $rule_num+100, $user_ip);
        exec($command2);
        $sql = ‘UPDATE users SET status=1, time_begin=NOW(), rule_num=’.$rule_num.’ WHERE id=’.$user[‘id’];
        mysql_query($sql);
        return true;
}
function checkip($ip)
{
        if (!$ip) return false;
        $user_ip = explode(‘.’, $ip);
        $check_ip = explode(‘.’, CLIENTS_IP_BEGIN);
        if (($check_ip[0] != $user_ip[0]) && $check_ip[0] != “*”) return false;
        if (($check_ip[1] != $user_ip[1]) && $check_ip[1] != “*”) return false;
        if (($check_ip[2] != $user_ip[2]) && $check_ip[2] != “*”) return false;
        if (!(($check_ip[3] <= $user_ip[3] && ($check_ip[3] + CLIENTS_IP_COUNT) >= $user_ip[3])) && $check_ip[3] != “*”) return false;
        return true;
}
?>

User’s closed script according to the end of time /usr/local/www/wifi/cron.php:

# <strong>cat /usr/local/www/wifi/cron.php</strong>
<strong>&lt;?php</strong>
<strong>require_once('config.php');</strong>
<strong>check_users();</strong>
<strong>function check_users()</strong>
<strong>{</strong>
<strong>        $sql = 'SELECT * FROM users WHERE status=1 AND time_begin &gt; 0 AND (TIME_TO_SEC(TIMEDIFF(NOW(), time_begin)) &gt; '.CLIENTS_TIME.')';</strong>
<strong>        $res = mysql_query($sql);</strong>
<strong>        if ($res)</strong>
<strong>        {</strong>
<strong>                while ($user = mysql_fetch_assoc($res))</strong>
<strong>                {</strong>
<strong>                        $command = sprintf(RULE_DEL_IP, $user['rule_num']);</strong>
<strong>                        exec($command);</strong>
<strong>                        $command2 = sprintf(RULE_DEL_IP2, $user['rule_num']+100);</strong>
<strong>                        exec($command2);</strong>
<strong>                        $sql = 'UPDATE users SET status=2, time_end=NOW() WHERE id='.$user['id'];</strong>
<strong>                        mysql_query($sql);</strong>
<strong>                }</strong>
<strong>        }</strong>
<strong>        return true;</strong>
<strong>}</strong>
<strong>?&gt;</strong>

The form for adding username and pasword (such as index.html file):

# <strong>cat /usr/local/www/wifi/index.html</strong>
<strong>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"</strong>
<strong>    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;</strong>
<strong>&lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"&gt;</strong>
<strong>&lt;head&gt;</strong>
<strong>        &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;</strong>
<strong>        &lt;title&gt;Administration&lt;/title&gt;</strong>
<strong>        &lt;link rel="stylesheet" type="text/css" href="admin.css" /&gt;</strong>
<strong>        &lt;!--[if lt IE 7]&gt;&lt;link rel="stylesheet" type="text/css" href="style-ie.css" /&gt;&lt;![endif]--&gt;</strong>
<strong>&lt;/head&gt;</strong>
<strong>&lt;body&gt;</strong>
<strong>&lt;div class="login"&gt;</strong>
<strong>&lt;div class="form"&gt;</strong>
<strong>&lt;form method="get" action="add.php"&gt;</strong>
<strong>        &lt;p&gt;&lt;label&gt;Login:&lt;/label&gt;&lt;input class="text" name="login" type="text" size="17"/&gt;&lt;/p&gt;</strong>
<strong>        &lt;p&gt;&lt;label&gt;Password:&lt;/label&gt;&lt;input class="text" name="pass" type="password" size="16"/&gt;&lt;/p&gt;</strong>
<strong>        &lt;p&gt;&lt;input class="submit" type="submit" value="Everything is OK!"/&gt;</strong>
<strong>&lt;/form&gt;</strong>
<strong>&lt;/div&gt;</strong>
<strong>&lt;div class="rules"&gt;</strong>
<strong>        &lt;h1&gt;Wi-Fi how to use&lt;/h1&gt;</strong>
<strong>        &lt;ol&gt;</strong>
<strong>                &lt;li&gt;It is free for guests!&lt;/li&gt;</strong>
<strong>                &lt;li&gt;Come to reception&lt;/li&gt;</strong>
<strong>                &lt;li&gt;Get username and password&lt;/li&gt;</strong>
<strong>                &lt;li&gt;Use wifi&lt;/li&gt;</strong>
<strong>        &lt;/ol&gt;</strong>
<strong>  &lt;/div&gt;</strong>
<strong>        &lt;/div&gt;</strong>
<strong>        &lt;/body&gt;</strong>
<strong>        &lt;/html&gt;</strong>

Administration panel will be in /usr/local/www/wifi/admin folder. We must protect this folder with htpasswd for security. The content of file /usr/local/www/wifi/admin/admin.php  will be as follows:

<strong>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"</strong>
<strong>  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;</strong>
<strong>&lt;html&gt;</strong>
<strong>        &lt;head&gt;</strong>
<strong>        &lt;title&gt;Administration panel&lt;/title&gt;</strong>
<strong>        &lt;link type="text/css" rel="stylesheet" href="style.css"&gt;</strong>
<strong>        &lt;/head&gt;</strong>
<strong>&lt;body&gt;</strong>
<strong>&lt;form method="post" action="admin.php"&gt;</strong>
<strong>        The amount of users: &lt;input type="text" value="" name="num" size=2&gt; count.&lt;br&gt;&lt;br&gt;</strong>
<strong>        &lt;input type="submit" value="Generate"&gt;</strong>
<strong>&lt;/form&gt;&lt;hr&gt;</strong>
<strong>&lt;?php</strong>
<strong>        require_once('/usr/local/www/wifi/config.php');</strong>
<strong>        $n = (int) $_POST['num'];</strong>
<strong>        if ($n &gt; 10) { echo '</strong><strong>The number of users that can be created is over limited!</strong> <strong>&lt;br&gt;&lt;br&gt;'; $n=0; }</strong>
<strong>  function generate_password($number=10)</strong>
<strong>  {</strong>
<strong>    $arr = array('1','2','3','4','5','6',</strong>
<strong>                 '7','8','9','0');</strong>
<strong>    // Generate the password</strong>
<strong>    $pass = "";</strong>
<strong>    for($i = 0; $i &lt; $number; $i++)</strong>
<strong>    {</strong>
//  Calculate random index of massive
<strong>$index = rand(0, count($arr) - 1);</strong>
<strong>      $pass .= $arr[$index];</strong>
<strong>    }</strong>
<strong>    return $pass;</strong>
<strong>  }</strong>
<strong>        for ($i=0; $i&lt;$n; $i++)</strong>
<strong>        {</strong>
<strong>                $login = generate_password(4);</strong>
<strong>                $pass = generate_password(6);</strong>
<strong>                $sql = 'INSERT INTO users (username, password, status, rule_num) VALUES("apt'.$login.'", "'.$pass.'", 0, 0)';</strong>
<strong>                $res = mysql_query($sql);</strong>
<strong>        }</strong>
<strong>        if ($res) echo 'Count of users &lt;b&gt;'.$n.'&lt;/b&gt; is added.&lt;br&gt;&lt;br&gt;';</strong>
<strong>        $sql = 'SELECT * FROM users';</strong>
<strong>        $res = mysql_query($sql);</strong>
<strong>        echo '&lt;table width=\'30%\'&gt;&lt;td&gt;&lt;b&gt;Username&lt;/b&gt;&lt;/td&gt;&lt;td&gt;&lt;b&gt;Password&lt;/b&gt;&lt;/td&gt;&lt;td&gt;&lt;b&gt;Status&lt;/b&gt;&lt;/td&gt;&lt;td&gt;&lt;b&gt;Rule&lt;/b&gt;&lt;/td&gt;';</strong>
<strong>        while ($data = mysql_fetch_assoc($res))</strong>
<strong>        {</strong>
<strong>                echo '&lt;tr&gt;';</strong>
<strong>                echo '&lt;td&gt;'.$data['username'].'&lt;/td&gt;';</strong>
<strong>                echo '&lt;td&gt;'.$data['password'].'&lt;/td&gt;';</strong>
<strong>                if ($data['status'] == 0) { echo '&lt;td class=\'blue\'&gt;Is not active&lt;/td&gt;'; }</strong>
<strong>                if ($data['status'] == 1) { echo '&lt;td class=\'green\'&gt;Used&lt;/td&gt;';</strong>
<strong>                echo '&lt;td&gt;'.$data['rule_num'].'&lt;/td&gt;';}</strong>
<strong>                if ($data['status'] == 2) { echo '&lt;td class=\'reds\'&gt;Was used&lt;/td&gt;'; }</strong>
<strong>                if ($data['status'] == 3) { echo '&lt;td&gt;&lt;b&gt;Was stopped&lt;/b&gt;&lt;/td&gt;'; }</strong>
<strong>                echo '&lt;/tr&gt;';</strong>
<strong>        }</strong>
<strong>        echo '&lt;/table&gt;';</strong>
<strong>?&gt;</strong>
<strong>&lt;/body&gt;</strong>
<strong>&lt;/html&gt;</strong>
<strong>/usr/local/www/wifi/admin/style.css</strong> content of the file will be as follows:
<strong>.reds {color:#f30;}</strong>
<strong>.blue {color:#0000cc;}</strong>
<strong>.green {color:#0f0;}</strong>

In the folder /usr/local/www/wifi we will create file named .htaccess and add the following lines.

<strong>AuthUserFile </strong><strong>/usr/local/www/wifi</strong><strong>/.htpasswd</strong>
<strong>AuthName "Soft Admin"</strong>
<strong>AuthType Basic</strong>
<strong>Require valid-user</strong>

We create user name and password in /usr/local/www/wifi folder:

<strong>htpasswd -bc .htpasswd admin freebsd</strong>

      – Write admin username and freebsd password in .htpasswd file
-b take username and password from the command line.
-c –Create the file which is shown and add to it(if it exists,it will remove and rewrite)

To check users limits we will add cron file our global /etc/crontab configuration file. This will check users each 1 minutes interval.

# <strong>echo "*/1    *    *    *    *    root    /usr/local/bin/php /usr/local/www/wifi/cron.php" &gt;&gt; /etc/crontab</strong>
# <strong>/etc/rc.d/cron restart</strong>

/usr/local/www/wifi/admin.css file’s content will be as follows(This file set the pictures which are in background). But the pictures is read from /usr/local/www/wifi/img/ folder. Pictures are /usr/local/www/wifi/img/gp.gif and /usr/local/www/wifi/img/logo.png (You can change these pictures to anywhere you want):

<strong>.login {width:800px; height:540px; position:absolute; left:50%; top:50%; margin:-250px 0 0 -400px; border:dashed 1px #ddd; background:url(img/logo.png) 30px 30px no-repeat #fff;}</strong>
<strong>.login .form {margin:120px 0 0 450px;}</strong>
<strong>.login .form p {position:relative; margin:0 0 30px 0;}</strong>
<strong>.login .form label {font:normal 18px arial; position:absolute; margin:3px 0 0 0; color:#aaa;}</strong>
<strong>.login .form input {margin:0 0 0 100px; padding:2px; font:normal 18px arial;}</strong>
<strong>.login .form input.text {border-right:solid 1px #ccc; border-bottom:solid 1px #ccc; border-left:solid 1px #888; border-top:solid 1px #888;}</strong>
<strong>.login .rules {padding:10px 20px; margin:50px 30px; background:url(img/gp.gif) 420px 20px no-repeat #ececec;}</strong>
<strong>h1 {margin:10px 0; font:normal 20px tahoma; color:#c00;}</strong>
<strong>ol {margin:20px 0 0 30px; padding:0;}</strong>
<strong>ol li {margin:0 0 10px 15px; font: normal 16px arial; }</strong>

We update all authorites in the folder, in order to set to all new files:

# <strong>chown -R unixmen:unixmen /usr/local/www/wifi/</strong>

As we enter the page We will be asked for username and password(we type admin  username and password that we created before and then push enter):
Admin-panel first page

The administrator interface will be as follows:
Admin-Panel

The user interface will be so:
Wifi-captive-portal

The source of used PHP codes is from http://lissyara.su
You can download source codes from here