#!/usr/bin/perl -Tw

use strict;
use DBI;

# radwho options
my $db_host = 'localhost';      # MySQL server
my $db_db = 'radiator';           # MySQL RADIUS database
my $db_user = 'root';               # MySQL username
my $db_pass = 'password';      # MySQL password
my $fingerd = 0;                # (0/1) be fingerd


# Globals
my $user = '';
my $nasipaddress = '';
my $dbh = '';
my $showconntype = 0;
my $showserial = 0;
my $showcallid = 0;


if ($fingerd) {
        $user = <STDIN>;
        if ($user) {
                chop($user);
                chop($user);
        }
} else {
        $user = shift;
        $nasipaddress = shift;
}


if ($user) {
        if ($user =~ /^\d+\.\d+\.\d+\.\d+/) {
                $nasipaddress = $user;
                $user = 0;
        } elsif ($user =~ /^-.*/) {
                push(@ARGV, $user);
                $user = 0;
        }
}

if ($nasipaddress) {
        if ($nasipaddress =~ /^-.*/) {
                push(@ARGV, $nasipaddress);
                $nasipaddress = 0;
        }
}

foreach my $arg (@ARGV) {
        if ($arg eq '-p') {
                $showconntype = 1;
        } elsif ($arg eq '-i') {
                $showserial = 1;
        } elsif ($arg eq '-c') {
                $showcallid = 1;
        } else {
                usage();
        }
}


$dbh = DBI->connect("DBI:mysql:$db_db:$db_host", $db_user, $db_pass)
|| die "Cannot connect to RADIUS database: $!";

my $sth = '';
my $sql = "SELECT * FROM RADONLINE";

$sql .= " AND UserName = '$user'" if $user;
$sql .= " AND NASIPAddress = '$nasipaddress'" if $nasipaddress;
$sql .= " ORDER BY UserName";

$sth = $dbh->prepare($sql) || die "Unable to prepare SQL
statement: ".$dbh->errstr();
$sth->execute || die "Unable to execute SQL statement".$dbh->errstr;


print "Login\tPhone\t\tWhat\tTTY\tStart\t\t\tFrom\t\tLocation\n";
while (my $rowref = $sth->fetchrow_hashref) {

        my $login = sprintf("%0.8s", $rowref->{USERNAME});
        my $name = sprintf("%0.16s", $rowref->{USERNAME});
        my $what = sprintf("%0.6s", $rowref->{LOGIN_TIME});
        my $tty = sprintf("S%0.6s", $rowref->{NASPORT});
        my $start = sprintf("%0.19s", $rowref->{LOGIN_TIME});
        my $from = sprintf("%0.15s", $rowref->{FRAMEDIPADDRESS});
        my $location = sprintf("%0.15s", $rowref->{NASIDENTIFIER});
        my $what1 = '';
        if ($showconntype) {
                if ($rowref->{NASPortType} eq 'ISDN') {
                        $what1 = ' I';
                } elsif ($rowref->{NASPortType} eq 'Async') {
                        $what1 = ' A';
                } elsif ($rowref->{NASPortType} eq 'Sync') {
                        $what1 = ' S';
                }
        }
        if ($showserial) {
                $name = sprintf("%.16s", $rowref->{AcctSessionId});
        }
        if ($showcallid) {
                $name = sprintf("%0.16s", $rowref->{CallingStationId});
        }

        print "$login\t$name\t";
        if (length($name) < 8) {
                print "\t";
        }
        print "$what$what1\t$tty\t$start\t$from\t";
        if (length($from) < 8) {
                print "\t";
        }
        print "$location\n";
}

$sth->finish;
$dbh->disconnect;

sub usage {
        print "Usage: radlast [user|nasipaddress] [nasipaddress]
[-p|-i|-c]\n";
        print "\tSpecify optional username and/or nasipaddress to view
specific records\n";
        print "\t-p Show protocol type\n";
        print "\t-i Show serial number\n";
        print "\t-c Show caller ID\n";
        exit;
}
