summaryrefslogtreecommitdiff
path: root/FS/bin/freeside-sqlradius-set-lastlog
blob: ad8563076d7a90ddd657ad82494af29e2c165808 (plain)
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
#!/usr/bin/perl -w

use strict;
use Getopt::Std;
use FS::UID qw(adminsuidsetup);
use FS::Record qw(qsearch qsearchs str2time_sql);
use FS::Conf;
use FS::part_export;
use FS::svc_acct;

my %allowed_types = map { $_ => 1 } qw ( sqlradius sqlradius_withdomain );
my $conf = new FS::Conf;

my $user = shift or die &usage;
adminsuidsetup $user;

my $export_x = shift;
my @part_export;
if ( !defined($export_x) ) {
  @part_export = qsearch('part_export', {} );
} elsif ( $export_x =~ /^(\d+)$/ ) {
  @part_export = qsearchs('part_export', { exportnum=>$1 } )
    or die "exportnum $export_x not found\n";
} else {
  @part_export = qsearch('part_export', { exporttype=>$export_x } )
    or die "no exports of type $export_x found\n";
}

# gross almost false laziness with FS::part_export::sqlradius::update_svc_acct
@part_export = grep { ! $_->option('ignore_accounting') }
               grep { $allowed_types{$_->exporttype} }
               @part_export
  or die "No sqlradius exports specified.";


foreach my $part_export ( @part_export ) {
  my $dbh = DBI->connect( map $part_export->option($_),
                           qw ( datasrc username password ) );

  my $str2time = str2time_sql( $dbh->{Driver}->{Name} );
  my $group = "UserName";
  $group .= ",Realm"
    if ( ref($part_export) =~ /withdomain/ );

  my $sth = $dbh->prepare("SELECT UserName, Realm,
                          $str2time max(AcctStartTime)),
                          $str2time max(AcctStopTime))
                          FROM radacct
                          WHERE AcctStartTime != 0 AND AcctStopTime != 0
                          GROUP BY $group")
    or die $dbh->errstr;
  $sth->execute() or die $sth->errstr;

  while (my $row = $sth->fetchrow_arrayref ) {
    my ($username, $realm, $start, $stop) = @$row;

    $username = lc($username) unless $conf->exists('username-uppercase');
    my $extra_sql = '';
    if ( ref($part_export) =~ /withdomain/ ) {
      $extra_sql = " And '$realm' = ( SELECT domain FROM svc_domain
                       WHERE svc_domain.svcnum = svc_acct.domsvc ) ";
    }

    my $svc_acct = qsearchs( 'svc_acct',
                             { 'username' => $username },
                             '',
                             $extra_sql,
                           );
    if ($svc_acct) {
      $svc_acct->last_login($start)
        if $start && (!$svc_acct->last_login || $start > $svc_acct->last_login);
      $svc_acct->last_logout($stop)
        if $stop && (!$svc_acct->last_logout || $stop > $svc_acct->last_logout);
    }
  }
}


sub usage {
  die "Usage:\n\n  freeside-sqlradius-set_lastlog user [ exportnum|exporttype ]\n";
}

=head1 NAME

freeside-sqlradius-set-lastlog - Command line tool to set last_login and last_logout values from radius tables

=head1 SYNOPSIS

  freeside-sqlradius-set-lastlog user [ exportnum|exporttype ]

=head1 DESCRIPTION

  Sets the last_login and last_logout columns of each svc_acct based on
  data in the radacct table for the specified export (selected by exportnum
  or exporttype) or all exports if none are specified.

=head1 SEE ALSO

L<freeside-sqlradius-radacctd>, L<FS::part_export> 

=cut