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
|
#!/usr/bin/perl
#
# $Id$
#
use warnings ;
use GDBM_File ;
use Fcntl ;
use Getopt::Long;
use File::Basename;
my $user = '';
my $divisor = 1;
my $reset = 0;
my $match = '.*';
my $help = 0;
#
# This should be fixed...
#
$filename = '';
sub show_help {
my $progname = basename($0);
print <<EOF;
Usage: $progname --file=<counter filename> [OPTION...]
Query and maintain FreeRADIUS rlm_counter DB file.
Arguments:
--file=<filename> Counter DB filename.
Options:
--user=<username> Information for specific user.
--match=<regexp> Information for matching users.
--reset=<number> Reset counter to <number>.
If divisor is set use it,
else <number> means seconds.
--help Show this help screen.
--(hours|minutes|seconds) Specify information divisor.
EOF
exit 0;
}
#
# Print out only one user,
#
# Or specify printing in hours, minutes, or seconds (default)
#
GetOptions ('user=s' => \$user,
'match=s' => \$match,
'file=s' => \$filename,
'reset=i' => \$reset,
'help' => \$help,
'hours' => sub { $divisor = 3600 },
'minutes' => sub { $divisor = 60 },
'seconds' => sub { $divisor = 1 } );
show_help if ($help || $filename eq '');
#
# Open the file.
#
if ($reset){
my $db = tie(%hash, 'GDBM_File', $filename, O_RDWR, 0666) or die "Cannot open $filename: $!\n";
}else{
my $db = tie(%hash, 'GDBM_File', $filename, O_RDONLY, 0666) or die "Cannot open $filename: $!\n";
}
#
# If given one name, give the seconds
#
if ($user ne '') {
if (defined($hash{$user})){
print $user, "\t\t", int ( unpack('L',$hash{$user}) / $divisor), "\n";
if ($reset){
my $uniqueid = (unpack('L A32',$hash{$user}))[1];
$hash{$user} = pack('L A32',$reset * $divisor,$uniqueid);
print $user, "\t\t", "Counter reset to ", $reset * $divisor, "\n";
}
}else{
print $user, "\t\t", "Not found\n";
}
undef $db;
untie %hash;
exit 0;
}
#
# This may be faster, but unordered.
#while (($key,$val) = each %hash) {
#
foreach $key (sort keys %hash) {
#
# These are special.
next if ($key eq "DEFAULT1");
next if ($key eq "DEFAULT2");
#
# Allow user names matching a regex.
#
next if ($key !~ /$match/);
#
# Print out the names...
print $key, "\t\t", int ( unpack('L',$hash{$key}) / $divisor), "\n";
if ($reset){
my $uniqueid = (unpack('L A32',$hash{$key}))[1];
$hash{$key} = pack('L A32',$reset * $divisor,$uniqueid);
print $key, "\t\t", "Counter reset to ", $reset * $divisor, "\n";
}
}
undef $db;
untie %hash;
|