Friday, July 30, 2010

Install ChilliSpot on CentOS 5

This will force users to login (via a captive portal web-page).

Info:

eth0 = WAN
eth1 = Internal Interface / LAN (Clients, PC, Access Points)

Chillispot takes control of (eth1) using a vtun kernel module
to bring up a virtual interface (tun0). The vtun kernel module
is used to move IP packets from the kernel to user mode

Chillispot sets up a DHCP server (can be disabled from the
chillispot conf file) on the tun0 interface.

A client connecting to internal interface has all packets rejected
until it is authorized though the chillispot login page (acting as
a supplicant for authentication). When a non-authenticated client
tries to connect to a web-page (on port 80 or 443) the request is
intercepted by chilli and redirected to a perl-script called
hotspotlogin.cgi (served by apache over https).

hotspotlogin.cgi serves a page to the end-user with a username and
password field. These authentication data are then forwarded to the
freeradius server, which matches them with information in it’s backend
(using either PAP or CHAP). The backend in this case is mysql, but
could be any number of services such as LDAP, Kerberos, unix passwd
files or even Active Directory (probably).

A user is then either rejected or authenticated by freeradius,
prompting hotspotlogin.cgi to present either a rejection message
or a page with a success message and a logout link to the user.


-------You need to install the following packages:
* mysql-server
* apache2
* freeradius
* freeradius-mysql


-------You need to enable packet forwarding:
Edit /etc/sysctl.conf and set net.ipv4.ip_forward = 1


-------Install chillispot from http://chillispot.org/download.html

1) Copy hotspotlogin.cgi from source to /var/www/cgi-bin directory
2) Copy chillispot-pf.conf from source to /etc/pf.conf
3) Edit /etc/pf.conf and update int & ext_if macros
4) Copy chilli.conf & chilli.ipup from source to /etc
5) Tell Chilli about the location of the authentication server
(which in this scenario is on the same machine as chillispot).
This is done by uncommenting and editing the following line in
“/etc/chilli.conf”:

uamserver https://192.168.182.1/cgi-bin/hotspotlogin.cgi

192.168.182.1 is the default IP address that chillispot gives
the tun0 interface.

6) For added password security, we need to add a shared secret
between the hotspotlogin.cgi and chilli. Find the line in
“/etc/chilli.conf” that reads

#uamsecret ht2eb8ej6s4et3rg1ulp

Uncomment this line (remove the #) and CHANGE the secret to
what ever you desire. The secret needs to be the same with the
hotspotlogin.cgi script.

Continue editing /etc/chilli.conf and update the dns, dhcpif
& other parameters.

Edit the hotspotlogin.cgi in your cgi-bin directory & update
the uamsecret so that its the same as the entry in your
/etc/chilli.conf. Also uncomment the line that reads:
#$userpassword=1;

7) chmod 755 /var/www/cgi-bin/hotspotlogin.cgi
8) Copy chilli.init from source to /etc/rc.d/init.d/chilli
Edit /etc/rc.d/init.d/chilli and Define the correct path
for the chilli binary


-------You need to configure the network interfaces.
1) Set eth0 for internet connection.
2) Set eth1 with address 0.0.0.0 255.255.255.0
or issue command: ifconfig eth1 0.0.0.0 up
3) Check that both interfaces are physically connected
to the appropriate network equipment


-------Configuring Apache2 for SSL
1) yum install mod_ssl
2) mkdir /etc/httpd/ssl
3) openssl req -new -x509 -days 365 -nodes -out \
/etc/httpd/ssl/httpd.pem -keyout /etc/httpd/ssl/httpd.key

4) Edit http.conf and enable ssl

NameVirtualHost *:443


SSLEngine On
SSLCertificateFile /etc/httpd/ssl/httpd.pem
SSLCertificateKeyFile /etc/httpd/ssl/httpd.key

#ServerAdmin info@mydomain.com
#ServerName www.mydomain.com
#DocumentRoot /srv/www/mydomain.com/public_html/
#ErrorLog /srv/www/mydomain.com/logs/error.log
#CustomLog /srv/www/mydomain.com/logs/access.log combined



-------Configure radius
1) Edit “/etc/raddb/clients.conf”.
Find the section that contains the line
client 127.0.0.1 {

make sure it is uncommented, and then, in the section between the
{ and the following }, change the following lines:

secret = testing123

change testing123 to match the radiussecret you chose for
“/etc/chilli.conf”


2) Edit “/etc/raddb/users”
Uncomment the following line in the file
#steve Auth-Type := Local, User-Password == “testing”
This will be the test user and password we will use to make sure
everything works.


-------Copy firewall.iptables from source to /etc/rc.d/init.d/chilli.iptables


-------Start the firewall
sh /etc/rc.d/init.d/chilli.iptables


-------Restart services
/etc/rc.d/init.d/httpd restart
/etc/rc.d/init.d/radiusd restart
/etc/rc.d/init.d/chilli restart


;notes

Thank this site for the information above:
http://www.multiplicity.dk/2006/10/chillispot-howto/

Wednesday, July 28, 2010

Query a database from Asterisk Part 2

This simple example resembles a feature, which might be used by credit card companies, where a user may inquire about recent payments.


Here are the configurations:
------------------------------------------------

;Create the database "odbc_finance_bank"
;Create table "finance_bank_members" with the information below

+------------------+----------+---------------------+--------------------+
| account_number | pin_code | recent_payment_date | recent_paid_amount |
+------------------+----------+---------------------+--------------------+
| 5111300001 | 1234 | 2010-05-16 19:23:00 | 5003.12 |
| 5222300002 | 5678 | 2010-06-22 10:09:00 | 9800.00 |
+------------------+----------+---------------------+--------------------+


;Edit configuration files
------------------------------------------------
;cat /etc/odbcinst.ini

[MySQL]
Description = ODBC for MySQL
Driver = /usr/lib/libmyodbc3.so
Setup = /usr/lib/libodbcmyS.so
FileUsage = 1

------------------------------------------------
;cat /etc/odbc.ini

[astodbc]
Description = Retrieve names and extensions
Driver = MySQL
Server = localhost
Port = 3306
USER = dbuser
Password = dbpassword
Database = odbc_finance_bank
Option = 3

------------------------------------------------
;cat /etc/asterisk/res_odbc.conf

[getuserinfo]
enabled => yes
dsn => astodbc
username => dbuser
password => dbpassword
pre-connect => yes

------------------------------------------------
;cat /etc/asterisk/func_odbc.conf

[GETDATE]
dsn=getuserinfo
read=SELECT recent_payment_date FROM finance_bank_members WHERE account_number='${SQL_ESC(${ARG1})}' and pin_code='${SQL_ESC(${ARG2})}';

[GETAMOUNT]
dsn=getuserinfo
read=SELECT recent_paid_amount FROM finance_bank_members WHERE account_number='${SQL_ESC(${ARG1})}' and pin_code='${SQL_ESC(${ARG2})}';

------------------------------------------------
;cat /etc/asterisk/extensions_custom.conf

[from-internal-custom]
; Account Transactions

; answer incoming
exten => 1000,1,answer
exten => 1000,n,wait(1)

; enter account number
exten => 1000,n,Playback(after-the-tone)
exten => 1000,n,Playback(please-enter-your)
exten => 1000,n,Playback(digits/10)
exten => 1000,n,Playback(astcc-digit-account-number)
exten => 1000,n,Playback(beep)
exten => 1000,n(getacctnum),read(account_number,,10,,3,15)

;confirm account number
exten => 1000,n,Playback(you-entered)
exten => 1000,n,Playback(silence/1)
exten => 1000,n,SayDigits(${account_number})
exten => 1000,n,Playback(silence/1)
exten => 1000,n,Playback(if-this-is-correct)
exten => 1000,n,Playback(press-1)
exten => 1000,n(acctverify),read(account_verify,,1,,3,5)
exten => 1000,n,GotoIf($[${account_verify} = 1]?getpincode:goodbye)

; enter pin code
exten => 1000,n(getpincode),Playback(please-enter-your)
exten => 1000,n,read(pin_code,access-code,4,,3,10)

; hangup if no data
exten => 1000,n,Set(recentdate=${ODBC_GETDATE(${account_number},${pin_code})})
exten => 1000,n,GotoIf($["${recentdate}" = ""]?tryagain:)

; if data present, format date
; recentdate format: 1976-12-16 07:30:35
exten => 1000,n,Set(formatdate=${STRPTIME(${recentdate}|Asia/Manila|%Y-%m-%d %H:%M:%S})

exten => 1000,n,Playback(received)
exten => 1000,n,SayUnixTime(${formatdate})
exten => 1000,n,Set(recentamount=${ODBC_GETAMOUNT(${account_number},${pin_code})})
exten => 1000,n,SayNumber(${recentamount})
exten => 1000,n,Playback(digits/dollars)
exten => 1000,n,Set(centavo=${CUT(recentamount,.,2)})
exten => 1000,n,Playback(and)
exten => 1000,n,SayNumber(${centavo})
exten => 1000,n,Playback(cents)

exten => 1000,n,Playback(silence/3)
exten => 1000,n(goodbye),Playback(goodbye)
exten => 1000,n,hangup()

exten => 1000,n,Playback(silence/3)
exten => 1000,n(tryagain),Playback(login-fail)
exten => 1000,n,Playback(please-try-again)
exten => 1000,n,Playback(goodbye)
exten => 1000,n,hangup()

Friday, July 23, 2010

Managing Cisco ATA 186

1) Reset the ATA to default configuration:
Power on ATA and insert an analog unit at "Phone 1" on the back
- Pick up handset
- Press red button to access the configuration menu
- You will hear "configuration menu"
- Enter: 322873738#
- You will hear "to save press star, or press the pound key"
- Press "*"
- Red light flashes. ATA is now at default configuration

2) Check the ATA's IP address (DHCP enabled):
- Access the configuration menu
- Type 80#

3) Change the ATA's IP address (DHCP disabled):
- Access the configuration menu
- Disable DHCP by entering:
20# 0# 3
1# 172*16*10*70 #3 - set IP address

10# 255*255*255*0 #3 - set mask

28# 172*16*10*1 - set default gateway
- Hang up the phone, device will reset with new IP address

Thursday, July 22, 2010

Query a database from Asterisk

; Define MySQL ODBC modules
; edit /etc/odbcinst.ini
[MySQL]
Description = ODBC for MySQL
Driver = /usr/lib/libmyodbc3.so
Setup = /usr/lib/libodbcmyS.so
FileUsage = 1

; edit /etc/odbc.ini
[astodbc]
Description = Retrieve names and extensions
Driver = MySQL
Server = localhost
Port = 3306
USER = astconnector
Password = astpasskey
Database = astodbc
Option = 3

; prepare asterisk for odbc connection
; asterisk will use res_odbc.conf information to connect to MySQL
; edit /etc/asterisk/res_odbc.conf
[sqlconnect]
enabled => yes
dsn => astodbc
username => astconnector
password => astpasskey
pre-connect => yes

; prepare the sql query commands that asterisk will initiate
; edit /etc/asterisk/func_odbc.conf
[GETUSERINFO]
dsn=sqlconnect
read=SELECT id_firstname, id_middlename, id_lastname, age, sex, location FROM client_db WHERE ext_number='${SQL_ESC(${ARG1})}'

; define extensions that connect to MySQL
; Festival needs to be configured prior to this
/etc/asterisk/extensions_custom.conf
[from-internal-custom]
exten => 5000,1,answer
exten => 5000,n,wait(1)
; get user input (ext_number)
exten => 5000,n(getextension),read(keyext,extension,4,,,10)
; say user input
exten => 5000,n,Festival(${keyext})
; retrieve information from odbc query in func_odbc.conf
exten => 5000,n,Set(myinfo=${ODBC_GETUSERINFO(${keyext})})
; say user information
exten => 5000,n,Festival(${myinfo})
exten => 5000,n,Playback(silence/3)
; ask for another input
exten => 5000,n,Goto(getextension)
exten => 5000,n,hangup()

sample sql table
select * from client_db ;
+----------+--------------+---------------+-------------+------------+-----+------+-----------+---------------------+
| uniqueid | id_firstname | id_middlename | id_lastname | ext_number | age | sex | location | stamp |
+----------+--------------+---------------+-------------+------------+-----+------+-----------+---------------------+
| 101 | John | Michael | Price | 4010 | 23 | male | home | 2010-03-12 03:30:42 |
| 103 | Joseph | Edward | Smith | 4020 | 25 | male | office | 2010-03-12 03:30:44 |
+----------+--------------+---------------+-------------+------------+-----+------+-----------+---------------------+

ViciDial - Number scrubbing / Call list washing

# Get CPD results from Campaigns
# CAMP1 - Campaign 1 name
# CAMP2 - Campaign 2 name
# List ID "537" - Number list common to both CAMP1 and CAMP2

# Run ViciDial with Sangoma Netborder support
# Then prepare to query the ViciDial database for CPD results

# Create table for CAMP1
create table scrubber_result_x_CAMP1 TYPE=MYISAM AS
select campaign_id, lead_id, list_id, term_reason, call_date,
length_in_sec as duration, phone_number, status from vicidial_log
where campaign_id = 'CAMP1' order by lead_id ASC;

# Add CPD column
alter table scrubber_result_x_CAMP1 add column cpd varchar(32);
update scrubber_result_x_CAMP1 set cpd = '' where cpd is null;

# Update CPD column
update scrubber_result_x_CAMP1 AS t1, vicidial_cpd_log AS t2
set t1.cpd = t2.result where t1.lead_id = t2.lead_id;

# OPTIONAL - Set NA for those w/o CPD results
update scrubber_result_x_CAMP1 set cpd = 'NA' where cpd = '';

# --------------------

# Create table for CAMP2
create table scrubber_result_x_CAMP2 TYPE=MYISAM AS
select campaign_id, lead_id, list_id, term_reason, call_date,
length_in_sec as duration, phone_number, status from vicidial_log
where campaign_id = 'CAMP2' order by lead_id ASC;

# Add CPD column
alter table scrubber_result_x_CAMP2 add column cpd varchar(32);
update scrubber_result_x_CAMP2 set cpd = '' where cpd is null;

# Update CPD column
update scrubber_result_x_CAMP2 AS t1, vicidial_cpd_log AS t2
set t1.cpd = t2.result where t1.lead_id = t2.lead_id;

# OPTIONAL - Set NA for those w/o CPD results
update scrubber_result_x_CAMP2 set cpd = 'NA' where cpd = '';

# --------------------

# Create comparison of two Campaigns (CAMP1 and CAMP2)

# Create Comparison table
create table scrubber_result_list_CAMP_COMPARISON TYPE=MYISAM AS
select lead_id, list_id, phone_number from vicidial_list
where list_id = '537' order by lead_id ASC;

# Add CPD column
alter table scrubber_result_list_CAMP_COMPARISON add column cpd varchar(32);
alter table scrubber_result_list_CAMP_COMPARISON add column cpd2 varchar(32);
update scrubber_result_list_CAMP_COMPARISON set cpd = '' where cpd is null;
update scrubber_result_list_CAMP_COMPARISON set cpd2 = '' where cpd2 is null;

# Update CPD column (lookup phone then update cpd)
update scrubber_result_list_CAMP_COMPARISON AS t1, scrubber_result_x_CAMP1 AS t2
set t1.cpd = t2.cpd where t1.phone_number = t2.phone_number;

update scrubber_result_list_CAMP_COMPARISON AS t1, scrubber_result_x_CAMP2 AS t2
set t1.cpd2 = t2.cpd where t1.phone_number = t2.phone_number;

update scrubber_result_list_CAMP_COMPARISON set cpd = 'NA' where cpd = '';
update scrubber_result_list_CAMP_COMPARISON set cpd2 = 'NA' where cpd2 = '';

select * from scrubber_result_list_CAMP_COMPARISON;

Monday, July 19, 2010

Backup and Restore Asterisk, FreePBX, MySQL, HTTP

;Required:
1) Make sure you have the same version of source files used to
install Asterisk, FreePBX, MySQL and Apache
2) Modify the file path if needed


;;;;Backup Script
#!/bin/sh
NOW=$(date +"%m-%d-%Y-%H%M%S")
#mkdir /root/ast-backup-$NOW
tar -zcvf /root/ast-backup-$NOW/etc-amportal-$NOW.tar.gz /etc/amportal.conf ;
tar -zcvf /root/ast-backup-$NOW/etc-asterisk-$NOW.tar.gz /etc/asterisk ;
tar -zcvf /root/ast-backup-$NOW/var-lib-asterisk-$NOW.tar.gz /var/lib/asterisk ;
tar -zcvf /root/ast-backup-$NOW/var-lib-mysql-$NOW.tar.gz /var/lib/mysql ;
tar -zcvf /root/ast-backup-$NOW/var-spool-asterisk-$NOW.tar.gz /var/spool/asterisk ;
tar -zcvf /root/ast-backup-$NOW/var-www-html-$NOW.tar.gz /var/www/html ;


;;;;Restore Script
#!/bin/sh
NOW=$(date +"%m-%d-%Y-%H%M%S")
######## ------- Configuration / DB restore steps -------
######## BACKUP EXISTING and RESTORE /etc/asterisk
mv /etc/asterisk /etc/asterisk.$NOW
cd /usr/local/src/postinstall/config_files/etc
cp -R asterisk /etc
chown -R asterisk.asterisk /etc/asterisk
cp amportal.conf /etc

######## RESTORE Directories
mv /var/www/html/admin /var/www/html/admin.$NOW
mv /var/www/html/myqueue /var/www/html/myqueue.$NOW
mv /var/www/html/panel /var/www/html/panel.$NOW
mv /var/www/html/recordings /var/www/html/recordings.$NOW

cp -R /usr/local/src/postinstall/config_files/var/www/html/* /var/www/html
chown -R asterisk.asterisk /var/www/html

mv /var/lib/mysql /var/lib/mysql.$NOW
cp -R /usr/local/src/postinstall/config_files/var/lib/mysql /var/lib
chown -R mysql.mysql /var/lib/mysql

mv /etc/asterisk /etc/asterisk.$NOW
cp -R /usr/local/src/postinstall/config_files/etc/asterisk /etc
chown -R asterisk.asterisk /etc/asterisk

mv /var/lib/asterisk /var/lib/asterisk.$NOW
cp -R /usr/local/src/postinstall/config_files/var/lib/asterisk /var/lib
chown -R asterisk.asterisk /var/lib/asterisk

mv /var/spool/asterisk /var/spool/asterisk.$NOW
cp -R /usr/local/src/postinstall/config_files/var/spool/asterisk /var/spool
chown -R asterisk.asterisk /var/spool/asterisk

######## RESTART MYSQL and HTTPD Services
/etc/rc.d/init.d/httpd restart
/etc/rc.d/init.d/mysqld restart