-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfreeswitch_ldap.pl
161 lines (132 loc) · 7.14 KB
/
freeswitch_ldap.pl
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/perl
use Net::LDAP;
# Set LDAP bind information here. Start with http://search.cpan.org/~gbarr/perl-ldap/lib/Net/LDAP.pod for more info on Net::LDAP
my $ldapserver = "localhost";
my $ldapversion = 3;
my $ldapuser = "uid=binduser,dc=domain,dc=com";
my $ldapbase = "ou=Contacts,dc=domain,dc=com";
my $ldappass = "password";
sub LDAPsearch {
my ($ldap,$searchString,$attrs,$base) = @_;
my $result = $ldap->search ( base => "$base",
scope => "sub",
filter => "$searchString",
attrs => $attrs
);
}
# This is where you would specify a different search field
my @attribs = ('cn', 'destinationIndicator');
# One of the first things I ran into was whether to call my script from an extension or at the
# dialplan. From the dialplan, you return a full XML dialplan in $XML_STRING as it is in the
# mod_perl docs. Try as I might, I never got Freeswitch to act on my dialplan, so I call the
# script from my dialplan as shown later in this wiki page. But, the takeaway is,
# if you generate a dialplan and call the script from perl.conf.xml, you have to use
# $params->getHeader. If you call the script as an application from an exception, you have to
# use $session->getVariable.
#my $number=$params->getHeader("Caller-Caller-ID-Number");
my $number=$session->getVariable("caller_id_number");
# I log the incoming number to the console to be sure my script is being called
freeswitch::console_log("info", "Handling call from $number\n");
# This will match on homePhone, mobile and telephoneNumber records in LDAP. You could also add
# the relevant fields for FAX numbers, or anything else that's in your schema. I used loose
# matching because that handles the case where a number appears as 1234567890 to Freeswitch, but
# could be entered as 123-456-7890 in LDAP.
my $searchstring="(|(homePhone~=$number)(mobile~=$number)(telephoneNumber~=$number))";
# Connect to the LDAP server and run our query
my $ldap = Net::LDAP->new ( $ldapserver ) or die "$@";
$ldap->bind ( $ldapuser, password => $ldappass, version => $ldapversion ) or die "$@";
my $search = LDAPsearch ($ldap, $searchstring, \@attribs, $ldapbase ) or die "$@";
my @entries = $search->entries;
my ($name, $dest);
# This could be done better - we simply grab the name of an extension out of LDAP,
# and transfer the call there. There's no handling of oddities in the LDAP search result
# and executing on data straight out of LDAP probably isn't the safest thing ever.
# If we have an extension listed in the contact's entry, we send the caller there,
# otherwise we send it to the greylist extension.
if (@entries) {
$name=$entries[0]->get_value('cn');
$dest=$entries[0]->get_value('destinationIndicator');
} else {
$name = "Unknown";
$dest="greylist";
}
# Freeswitch picks up the call here, sets the CID name and number (which doesn't seem to have
# an effect for me, and transfers the call to the extension the script picked.
$session->answer();
$session->setVariable("effective_caller_id_number", $number);
$session->setVariable("effective_caller_id_name", $name);
$session->transfer($dest, "XML", "default");
# Freeswitch will be mad if you don't return a true value.
1;
Now, we need to add entries to the XML files. Starting with the default configuration files from when I compiled Freeswitch, I added this to conf/dialplan/public.xml:
<extension name="ldap_switched_in">
<condition field="destination_number" expression="MYDID">
<action application="perl" data="ldaplookup.pl"/>
</condition>
</extension>
#!/usr/bin/perl
use Net::LDAP;
# Set LDAP bind information here. Start with http://search.cpan.org/~gbarr/perl-ldap/lib/Net/LDAP.pod for more info on Net::LDAP
my $ldapserver = "localhost";
my $ldapversion = 3;
my $ldapuser = "uid=binduser,dc=domain,dc=com";
my $ldapbase = "ou=Contacts,dc=domain,dc=com";
my $ldappass = "password";
sub LDAPsearch {
my ($ldap,$searchString,$attrs,$base) = @_;
my $result = $ldap->search ( base => "$base",
scope => "sub",
filter => "$searchString",
attrs => $attrs
);
}
# This is where you would specify a different search field
my @attribs = ('cn', 'destinationIndicator');
# One of the first things I ran into was whether to call my script from an extension or at the
# dialplan. From the dialplan, you return a full XML dialplan in $XML_STRING as it is in the
# mod_perl docs. Try as I might, I never got Freeswitch to act on my dialplan, so I call the
# script from my dialplan as shown later in this wiki page. But, the takeaway is,
# if you generate a dialplan and call the script from perl.conf.xml, you have to use
# $params->getHeader. If you call the script as an application from an exception, you have to
# use $session->getVariable.
#my $number=$params->getHeader("Caller-Caller-ID-Number");
my $number=$session->getVariable("caller_id_number");
# I log the incoming number to the console to be sure my script is being called
freeswitch::console_log("info", "Handling call from $number\n");
# This will match on homePhone, mobile and telephoneNumber records in LDAP. You could also add
# the relevant fields for FAX numbers, or anything else that's in your schema. I used loose
# matching because that handles the case where a number appears as 1234567890 to Freeswitch, but
# could be entered as 123-456-7890 in LDAP.
my $searchstring="(|(homePhone~=$number)(mobile~=$number)(telephoneNumber~=$number))";
# Connect to the LDAP server and run our query
my $ldap = Net::LDAP->new ( $ldapserver ) or die "$@";
$ldap->bind ( $ldapuser, password => $ldappass, version => $ldapversion ) or die "$@";
my $search = LDAPsearch ($ldap, $searchstring, \@attribs, $ldapbase ) or die "$@";
my @entries = $search->entries;
my ($name, $dest);
# This could be done better - we simply grab the name of an extension out of LDAP,
# and transfer the call there. There's no handling of oddities in the LDAP search result
# and executing on data straight out of LDAP probably isn't the safest thing ever.
# If we have an extension listed in the contact's entry, we send the caller there,
# otherwise we send it to the greylist extension.
if (@entries) {
$name=$entries[0]->get_value('cn');
$dest=$entries[0]->get_value('destinationIndicator');
} else {
$name = "Unknown";
$dest="greylist";
}
# Freeswitch picks up the call here, sets the CID name and number (which doesn't seem to have
# an effect for me, and transfers the call to the extension the script picked.
$session->answer();
$session->setVariable("effective_caller_id_number", $number);
$session->setVariable("effective_caller_id_name", $name);
$session->transfer($dest, "XML", "default");
# Freeswitch will be mad if you don't return a true value.
1;
Now, we need to add entries to the XML files. Starting with the default configuration files from when I compiled Freeswitch, I added this to conf/dialplan/public.xml:
<extension name="ldap_switched_in">
<condition field="destination_number" expression="MYDID">
<action application="perl" data="ldaplookup.pl"/>
</condition>
</extension>