Matthew Simpson
2004-Jul-02 16:30 UTC
[Asterisk-Users] DISA and AGI: authenticate by caller ID? (resolved)
Here is some code to do authentication by caller ID for DISA through AGI. My original code had a bug in the Mysql query code, and there was a hangup in the wrong place [that's what I get for coding something at 2:00am], but the attached code works correctly. Take note of the REGEXP for the CallerID variable. When I tested the code from the PSTN it worked because there was no name component, but it broke from SIP. If you're calling SIP-->SIP you'll have the name in that variable as well as the number, so I added code to snip everything but the 10 digits. Adjust accordingly if you have more or less than 10 digits. Also, I've thought of a bug already, if your caller ID name has digits in it, it'll break the regexp. Adjust accordingly if that is true about your installation. Yours, Matthew Simpson TxLink Communications IAX/SIP Termination and Origination Wholesale Dialup Services matthew@txlink.net 972-617-2877 http://www.txlink.net You'll need a context called ldincoming [or equivalent] for the AGI to transfer access to DISA like: [ldincoming] exten => 1011,1,DISA(no-password|disa) exten => 1011,2,Hangup You'll need a context called disa [or equivalent] with what you want to allow the authenticated callers to access, mine looks like: [disa] include => tollfree include => localonly and then just call the agi in your dialplan with something like: 1234,1,AGI(cidauth.agi) 1234,2,Hangup Here is the Mysql table: mysql> describe cids; +--------+----------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------+----------+------+-----+---------+----------------+ | id | int(11) | | PRI | NULL | auto_increment | | cid | char(10) | YES | | NULL | | | active | int(11) | YES | | NULL | | +--------+----------+------+-----+---------+----------------+ Insert each CID into the table that you want to have access. Active = 0 for disabled, active = 1 for enabled. Here is the perl code: #!/usr/bin/perl # use Asterisk::AGI; use DBI; $db = "dbname"; $host = "dbhost"; $port = "3306"; $userid = "dbuser"; $password = "dbpass"; $connectionInfo = "DBI:mysql:database=$db;$host:$port"; $dbh = DBI->connect($connectionInfo,$userid,$password); $AGI = new Asterisk::AGI; my %input = $AGI->ReadParse(); $AGI->answer(); if (my $callerid = $input{'callerid'}) { $callerid =~ m/(\d{10})/; # cut off the name part of CID, numbers only $callerid = $1; $query = "SELECT active FROM cids WHERE cid='$callerid'"; $sth = $dbh->prepare($query); $sth->execute(); $active = $sth->fetchrow_hashref(); if ($active->{"active"}) { $AGI->set_context('ldincoming'); $AGI->set_extension('1011'); $AGI->set_priority(1); exit; } } # if we got here, there was no match found [auth failed], so play a message saying so # you could also log all auth failed [with caller ID ! :) ] # you could also transfer caller to an operator $AGI->stream_file('invalid'); $AGI->hangup(); exit;