default to a session cookie instead of setting an explicit timeout, weird timezone...
[freeside.git] / FS / bin / freeside-sqlradius-set-lastlog
1 #!/usr/bin/perl -w
2
3 use strict;
4 use Getopt::Std;
5 use FS::DBI;
6 use FS::UID qw(adminsuidsetup);
7 use FS::Record qw(qsearch qsearchs str2time_sql);
8 use FS::Conf;
9 use FS::part_export;
10 use FS::svc_acct;
11
12 my %allowed_types = map { $_ => 1 } qw ( sqlradius sqlradius_withdomain );
13 my $conf = new FS::Conf;
14
15 my $user = shift or die &usage;
16 adminsuidsetup $user;
17
18 my $export_x = shift;
19 my @part_export;
20 if ( !defined($export_x) ) {
21   @part_export = qsearch('part_export', {} );
22 } elsif ( $export_x =~ /^(\d+)$/ ) {
23   @part_export = qsearchs('part_export', { exportnum=>$1 } )
24     or die "exportnum $export_x not found\n";
25 } else {
26   @part_export = qsearch('part_export', { exporttype=>$export_x } )
27     or die "no exports of type $export_x found\n";
28 }
29
30 # gross almost false laziness with FS::part_export::sqlradius::update_svc_acct
31 @part_export = grep { ! $_->option('ignore_accounting') }
32                grep { $allowed_types{$_->exporttype} }
33                @part_export
34   or die "No sqlradius exports specified.";
35
36
37 foreach my $part_export ( @part_export ) {
38   my $dbh = FS::DBI->connect( map $part_export->option($_),
39                            qw ( datasrc username password ) );
40
41   my $str2time = str2time_sql( $dbh->{Driver}->{Name} );
42   my $group = "UserName";
43   $group .= ",Realm"
44     if ( ref($part_export) =~ /withdomain/ );
45
46   my $sth = $dbh->prepare("SELECT UserName, Realm,
47                           $str2time max(AcctStartTime)),
48                           $str2time max(AcctStopTime))
49                           FROM radacct
50                           WHERE AcctStartTime != 0 AND AcctStopTime != 0
51                           GROUP BY $group")
52     or die $dbh->errstr;
53   $sth->execute() or die $sth->errstr;
54
55   while (my $row = $sth->fetchrow_arrayref ) {
56     my ($username, $realm, $start, $stop) = @$row;
57
58     $username = lc($username) unless $conf->exists('username-uppercase');
59     my $extra_sql = '';
60     if ( ref($part_export) =~ /withdomain/ ) {
61       $extra_sql = " And '$realm' = ( SELECT domain FROM svc_domain
62                        WHERE svc_domain.svcnum = svc_acct.domsvc ) ";
63     }
64
65     my $svc_acct = qsearchs( 'svc_acct',
66                              { 'username' => $username },
67                              '',
68                              $extra_sql,
69                            );
70     if ($svc_acct) {
71       $svc_acct->last_login($start)
72         if $start && (!$svc_acct->last_login || $start > $svc_acct->last_login);
73       $svc_acct->last_logout($stop)
74         if $stop && (!$svc_acct->last_logout || $stop > $svc_acct->last_logout);
75     }
76   }
77 }
78
79
80 sub usage {
81   die "Usage:\n\n  freeside-sqlradius-set_lastlog user [ exportnum|exporttype ]\n";
82 }
83
84 =head1 NAME
85
86 freeside-sqlradius-set-lastlog - Command line tool to set last_login and last_logout values from radius tables
87
88 =head1 SYNOPSIS
89
90   freeside-sqlradius-set-lastlog user [ exportnum|exporttype ]
91
92 =head1 DESCRIPTION
93
94   Sets the last_login and last_logout columns of each svc_acct based on
95   data in the radacct table for the specified export (selected by exportnum
96   or exporttype) or all exports if none are specified.
97
98 =head1 SEE ALSO
99
100 L<freeside-sqlradius-radacctd>, L<FS::part_export> 
101
102 =cut
103