3 # Log failed logins in the sql database
4 # Works with mysql and postgresql
5 # It will read the sql parameters from the admin.conf file
8 # log_badlogins <radius.log> [<admin.conf>] [all]
12 # admin.conf: /usr/local/dialup_admin/conf/admin.conf
13 # all: no. Go to the end of the file. Don't read it all.
15 use Date::Manip qw(ParseDate UnixDate);
20 $conf=shift||'/usr/local/dialup_admin/conf/admin.conf';
21 $all_file=shift||'no';
23 # Uncomment to force inserts even if there are sql errors. That can
24 # help in case there is one sql query which stops the whole failed
25 # logins logging system from working
29 # CHANGE THESE TO MATCH YOUR SETUP
31 #$regexp = 'from client localhost port 135|from client blabla ';
32 $tmpfile='/var/tmp/sql.input';
37 or die "Could not open configuration file\n";
40 ($key,$val)=(split /:\s*/,$_);
41 # Fixme : recursivly solve %{.*} replacement for $val
42 # Fixme: Conf should be put in an associative array
43 $sql_type = $val if ($key eq 'sql_type');
44 $sql_server = $val if ($key eq 'sql_server');
45 $sql_username = $val if ($key eq 'sql_username');
46 $sql_password = $val if ($key eq 'sql_password');
47 $sql_database = $val if ($key eq 'sql_database');
48 $sql_accounting_table = $val if ($key eq 'sql_accounting_table');
49 $realm_strip = $val if ($key eq 'general_strip_realms');
50 $realm_del = $val if ($key eq 'general_realm_delimiter');
51 $realm_for = $val if ($key eq 'general_realm_format');
52 $domain = $val if ($key eq 'general_domain');
53 $sql_timeout = $val if ($key eq 'sql_connect_timeout');
54 $sql_extra = $val if ($key eq 'sql_extra_servers');
55 $sqlcmd = $val if ($key eq 'sql_command');
56 $clients= $val if ($key eq 'general_clients_conf');
60 open CLIENTS, "<$clients"
61 or die "Could not open $clients file\n";
66 if (!/^\s*$/ && /=/) {
67 ($key,$val)=(split /\s*=\s*/,$_);
68 $client_short = $val if ($key eq 'shortname');
71 s/.*client\s+([^\s]*)\s+\{.*$/\1/;
72 if (/^\d+\.\d+\.\d+\.\d+/) {
75 if (/\./ || /localhost/) {
78 $name = $_.".".$domain;
80 $addr = gethostbyname $name;
81 ($a,$b,$c,$d)=unpack('C4',$addr);
82 $client = "$a.$b.$c.$d";
83 #DEBUG# print $name." = ".$client."\n";
87 $client_array{$client_short} .= $client;
94 $realm_del = '@' if ($realm_del eq '');
95 $realm_for = 'suffix' if ($realm_for eq '');
96 $pass = ($sql_password ne '') ? "-p$sql_password" : '';
97 die "SQL server not defined\n" if ($sql_server eq '');
99 die "sql_command directive is not set in admin.conf\n" if ($sqlcmd eq '');
100 die "Could not find sql binary. Please make sure that the \$sqlcmd variable points to the right location\n" if (! -x $sqlcmd);
103 $opt = "-O connect_timeout=$sql_timeout" if ($sql_timeout);
104 $opt .= " -f" if ($force);
105 @servers = (split /\s+/,$sql_extra) if ($sql_extra ne '');
106 unshift @servers, $sql_server;
109 or die "Could not open file $file\n";
111 seek LOG, 0, 2 if ($all_file eq 'no');
116 next if ($regexp ne '' && !/$regexp/);
118 $user = $nas = $port = $caller = '-';
119 if (/Login incorrect/){
120 if (/Login incorrect \((.+?)\):/){
121 $cause = "Login-Incorrect ($1)";
123 $cause='Login-Incorrect';
127 elsif (/Invalid user/){
128 if (/Invalid user \((.+?)\):/){
129 $cause = "Invalid-User ($1)";
131 $cause='Invalid-User';
135 elsif (/Multiple logins/){
137 $cause='Multiple-Logins (MPP Attempt)';
139 $cause='Multiple-Logins';
143 elsif (/(Outside allowed timespan \(.+?\)):/){
148 $date = (split / : /,$_)[0];
149 $date2 = ParseDate($date);
151 ($year,$mon,$mday,$hour,$min,$sec)=UnixDate($date2,'%Y','%m','%d','%H','%M','%S');
153 $time = "$year-$mon-$mday $hour:$min:$sec";
154 if (/\[([\w\-\.\!\@\s]+?)\]\s+\(from (.+?)\)/){
156 ($nas,$port) = (split /\s+/,$2)[1,3];
157 if ($2 =~ /cli (.+?)$/){
161 elsif (/\[([\w\-\.\!\@\s]+?)\/.+?\]\s+\(from (.+?)\)/){
163 ($nas,$port) = (split /\s+/,$2)[1,3];
164 if ($2 =~ /cli (.+?)$/){
168 $caller='' if (!defined($caller));
169 $user =~s/[^\w\-\.\d\!\@\s]//g;
170 $nas =~s/[^\w\.\-]//g;
172 $addr = $client_array{$nas};
173 if ($user ne '' && $realm_strip eq 'yes'){
174 ($one,$two) = (split /$realm_del/, $user)[0,1];
176 $user = ($realm_for eq 'suffix') ? $one : $two;
179 foreach $server (@servers){
180 unlink "$tmpfile.$server" if ($delete{$server});
181 open TMP, ">>$tmpfile.$server"
182 or die "Could not open temporary file\n";
183 $ctx = Digest::MD5->new;
188 $ctx->add('badlogin');
189 $uniqueid = $ctx->hexdigest;
190 #DEBUG# print "INSERT INTO $sql_accounting_table (UserName,AcctUniqueId,NASIPAddress,NASPortId,AcctStartTime,AcctStopTime,AcctSessionTime,AcctInputOctets,AcctOutputOctets,CallingStationId,AcctTerminateCause) VALUES ('$user','$uniqueid','$addr','$port','$time','$time','0','0','0','$caller','$cause');\n";
191 print TMP "INSERT INTO $sql_accounting_table (UserName,AcctUniqueId,NASIPAddress,NASPortId,AcctStartTime,AcctStopTime,AcctSessionTime,AcctInputOctets,AcctOutputOctets,CallingStationId,AcctTerminateCause) VALUES ('$user','$uniqueid','$addr','$port','$time','$time','0','0','0','$caller','$cause');\n";
193 $command = "$sqlcmd -h$server $opt -u$sql_username $pass $sql_database <$tmpfile.$server" if ($sql_type eq 'mysql');
194 $command = "$sqlcmd -U $sql_username -f $tmpfile.$server $sql_database" if ($sql_type eq 'pg');
198 $delete{$server} = ($exit == 0) ? 1 : 0;
199 print "ERROR: SQL query failed for host $server\n" if ($exit != 0);
204 if ($all_file ne 'once') {