1 package FS::part_export::sqlradius;
3 use vars qw(@ISA @EXPORT_OK $DEBUG %info %options $notes1 $notes2);
6 use FS::Record qw( dbh qsearch qsearchs str2time_sql );
12 @ISA = qw(FS::part_export);
13 @EXPORT_OK = qw( sqlradius_connect );
18 tie %options, 'Tie::IxHash',
19 'datasrc' => { label=>'DBI data source ' },
20 'username' => { label=>'Database username' },
21 'password' => { label=>'Database password' },
22 'usergroup' => { label => 'Group table',
24 options => [qw( usergroup radusergroup ) ],
26 'ignore_accounting' => {
28 label => 'Ignore accounting records from this database'
30 'process_single_realm' => {
32 label => 'Only process one realm of accounting records',
34 'realm' => { label => 'The realm of of accounting records to be processed' },
35 'ignore_long_sessions' => {
37 label => 'Ignore sessions which span billing periods',
41 label => 'Hide IP address information on session reports',
45 label => 'Hide download/upload information on session reports',
47 'show_called_station' => {
49 label => 'Show the Called-Station-ID on session reports',
51 'overlimit_groups' => {
52 label => 'Radius groups to assign to svc_acct which has exceeded its bandwidth or time limit (if not overridden by overlimit_groups global or per-agent config)',
58 option_values => sub {
60 map { $_->groupnum, $_->long_description }
61 qsearch('radius_group', {}),
66 'groups_susp_reason' => { label =>
67 'Radius group mapping to reason (via template user) (svcnum|username|username@domain reasonnum|reason)',
74 Real-time export of <b>radcheck</b>, <b>radreply</b> and <b>usergroup</b>/<b>radusergroup</b>
75 tables to any SQL database for
76 <a href="http://www.freeradius.org/">FreeRADIUS</a>
77 or <a href="http://radius.innercite.com/">ICRADIUS</a>.
81 An existing RADIUS database will be updated in realtime, but you can use
82 <a href="http://www.freeside.biz/mediawiki/index.php/Freeside:1.9:Documentation:Developer/bin/freeside-sqlradius-reset">freeside-sqlradius-reset</a>
83 to delete the entire RADIUS database and repopulate the tables from the
84 Freeside database. See the
85 <a href="http://search.cpan.org/dist/DBI/DBI.pm#connect">DBI documentation</a>
87 <a href="http://search.cpan.org/search?mode=module&query=DBD%3A%3A">documentation for your DBD</a>
88 for the exact syntax of a DBI data source.
90 <li>Using FreeRADIUS 0.9.0 with the PostgreSQL backend, the db_postgresql.sql schema and postgresql.conf queries contain incompatible changes. This is fixed in 0.9.1. Only new installs with 0.9.0 and PostgreSQL are affected - upgrades and other database backends and versions are unaffected.
91 <li>Using ICRADIUS, add a dummy "op" column to your database:
93 ALTER TABLE radcheck ADD COLUMN op VARCHAR(2) NOT NULL DEFAULT '=='<br>
94 ALTER TABLE radreply ADD COLUMN op VARCHAR(2) NOT NULL DEFAULT '=='<br>
95 ALTER TABLE radgroupcheck ADD COLUMN op VARCHAR(2) NOT NULL DEFAULT '=='<br>
96 ALTER TABLE radgroupreply ADD COLUMN op VARCHAR(2) NOT NULL DEFAULT '=='
98 <li>Using Radiator, see the
99 <a href="http://www.open.com.au/radiator/faq.html#38">Radiator FAQ</a>
100 for configuration information.
106 'desc' => 'Real-time export to SQL-backed RADIUS (FreeRADIUS, ICRADIUS)',
107 'options' => \%options,
109 'nas' => 'Y', # show export_nas selection in UI
111 'This export does not export RADIUS realms (see also '.
112 'sqlradius_withdomain). '.
116 sub _groups_susp_reason_map { map { reverse( /^\s*(\S+)\s*(.*)$/ ) }
117 split( "\n", shift->option('groups_susp_reason'));
120 sub rebless { shift; }
122 sub export_username { # override for other svcdb
123 my($self, $svc_acct) = (shift, shift);
124 warn "export_username called on $self with arg $svc_acct" if $DEBUG > 1;
128 sub radius_reply { #override for other svcdb
129 my($self, $svc_acct) = (shift, shift);
130 $svc_acct->radius_reply;
133 sub radius_check { #override for other svcdb
134 my($self, $svc_acct) = (shift, shift);
135 $svc_acct->radius_check;
139 my($self, $svc_x) = (shift, shift);
141 foreach my $table (qw(reply check)) {
142 my $method = "radius_$table";
143 my %attrib = $self->$method($svc_x);
144 next unless keys %attrib;
145 my $err_or_queue = $self->sqlradius_queue( $svc_x->svcnum, 'insert',
146 $table, $self->export_username($svc_x), %attrib );
147 return $err_or_queue unless ref($err_or_queue);
149 my @groups = $svc_x->radius_groups;
151 cluck localtime(). ": queuing usergroup_insert for ". $svc_x->svcnum.
152 " (". $self->export_username($svc_x). " with ". join(", ", @groups)
154 my $usergroup = $self->option('usergroup') || 'usergroup';
155 my $err_or_queue = $self->sqlradius_queue(
156 $svc_x->svcnum, 'usergroup_insert',
157 $self->export_username($svc_x), $usergroup, @groups );
158 return $err_or_queue unless ref($err_or_queue);
163 sub _export_replace {
164 my( $self, $new, $old ) = (shift, shift, shift);
166 local $SIG{HUP} = 'IGNORE';
167 local $SIG{INT} = 'IGNORE';
168 local $SIG{QUIT} = 'IGNORE';
169 local $SIG{TERM} = 'IGNORE';
170 local $SIG{TSTP} = 'IGNORE';
171 local $SIG{PIPE} = 'IGNORE';
173 my $oldAutoCommit = $FS::UID::AutoCommit;
174 local $FS::UID::AutoCommit = 0;
178 if ( $self->export_username($old) ne $self->export_username($new) ) {
179 my $usergroup = $self->option('usergroup') || 'usergroup';
180 my $err_or_queue = $self->sqlradius_queue( $new->svcnum, 'rename',
181 $self->export_username($new), $self->export_username($old), $usergroup );
182 unless ( ref($err_or_queue) ) {
183 $dbh->rollback if $oldAutoCommit;
184 return $err_or_queue;
186 $jobnum = $err_or_queue->jobnum;
189 foreach my $table (qw(reply check)) {
190 my $method = "radius_$table";
191 my %new = $new->$method();
192 my %old = $old->$method();
193 if ( grep { !exists $old{$_} #new attributes
194 || $new{$_} ne $old{$_} #changed
197 my $err_or_queue = $self->sqlradius_queue( $new->svcnum, 'insert',
198 $table, $self->export_username($new), %new );
199 unless ( ref($err_or_queue) ) {
200 $dbh->rollback if $oldAutoCommit;
201 return $err_or_queue;
204 my $error = $err_or_queue->depend_insert( $jobnum );
206 $dbh->rollback if $oldAutoCommit;
212 my @del = grep { !exists $new{$_} } keys %old;
214 my $err_or_queue = $self->sqlradius_queue( $new->svcnum, 'attrib_delete',
215 $table, $self->export_username($new), @del );
216 unless ( ref($err_or_queue) ) {
217 $dbh->rollback if $oldAutoCommit;
218 return $err_or_queue;
221 my $error = $err_or_queue->depend_insert( $jobnum );
223 $dbh->rollback if $oldAutoCommit;
231 my (@oldgroups) = $old->radius_groups;
232 my (@newgroups) = $new->radius_groups;
233 $error = $self->sqlreplace_usergroups( $new->svcnum,
234 $self->export_username($new),
235 $jobnum ? $jobnum : '',
240 $dbh->rollback if $oldAutoCommit;
244 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
249 sub _export_suspend {
250 my( $self, $svc_acct ) = (shift, shift);
252 my $new = $svc_acct->clone_suspended;
254 local $SIG{HUP} = 'IGNORE';
255 local $SIG{INT} = 'IGNORE';
256 local $SIG{QUIT} = 'IGNORE';
257 local $SIG{TERM} = 'IGNORE';
258 local $SIG{TSTP} = 'IGNORE';
259 local $SIG{PIPE} = 'IGNORE';
261 my $oldAutoCommit = $FS::UID::AutoCommit;
262 local $FS::UID::AutoCommit = 0;
265 my @newgroups = $self->suspended_usergroups($svc_acct);
267 unless (@newgroups) { #don't change password if assigning to a suspended group
269 my $err_or_queue = $self->sqlradius_queue( $new->svcnum, 'insert',
270 'check', $self->export_username($new), $new->radius_check );
271 unless ( ref($err_or_queue) ) {
272 $dbh->rollback if $oldAutoCommit;
273 return $err_or_queue;
279 $self->sqlreplace_usergroups( $new->svcnum,
280 $self->export_username($new),
282 [ $svc_acct->radius_groups ],
286 $dbh->rollback if $oldAutoCommit;
289 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
294 sub _export_unsuspend {
295 my( $self, $svc_acct ) = (shift, shift);
297 local $SIG{HUP} = 'IGNORE';
298 local $SIG{INT} = 'IGNORE';
299 local $SIG{QUIT} = 'IGNORE';
300 local $SIG{TERM} = 'IGNORE';
301 local $SIG{TSTP} = 'IGNORE';
302 local $SIG{PIPE} = 'IGNORE';
304 my $oldAutoCommit = $FS::UID::AutoCommit;
305 local $FS::UID::AutoCommit = 0;
308 my $err_or_queue = $self->sqlradius_queue( $svc_acct->svcnum, 'insert',
309 'check', $self->export_username($svc_acct), $svc_acct->radius_check );
310 unless ( ref($err_or_queue) ) {
311 $dbh->rollback if $oldAutoCommit;
312 return $err_or_queue;
316 my (@oldgroups) = $self->suspended_usergroups($svc_acct);
317 $error = $self->sqlreplace_usergroups( $svc_acct->svcnum,
318 $self->export_username($svc_acct),
321 [ $svc_acct->radius_groups ],
324 $dbh->rollback if $oldAutoCommit;
327 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
333 my( $self, $svc_x ) = (shift, shift);
334 my $usergroup = $self->option('usergroup') || 'usergroup';
335 my $err_or_queue = $self->sqlradius_queue( $svc_x->svcnum, 'delete',
336 $self->export_username($svc_x), $usergroup );
337 ref($err_or_queue) ? '' : $err_or_queue;
340 sub sqlradius_queue {
341 my( $self, $svcnum, $method ) = (shift, shift, shift);
342 my $queue = new FS::queue {
344 'job' => "FS::part_export::sqlradius::sqlradius_$method",
347 $self->option('datasrc'),
348 $self->option('username'),
349 $self->option('password'),
354 sub suspended_usergroups {
355 my ($self, $svc_acct) = (shift, shift);
357 return () unless $svc_acct;
359 #false laziness with FS::part_export::shellcommands
360 #subclass part_export?
362 my $r = $svc_acct->cust_svc->cust_pkg->last_reason('susp');
363 my %reasonmap = $self->_groups_susp_reason_map;
366 $userspec = $reasonmap{$r->reasonnum}
367 if exists($reasonmap{$r->reasonnum});
368 $userspec = $reasonmap{$r->reason}
369 if (!$userspec && exists($reasonmap{$r->reason}));
372 if ($userspec =~ /^\d+$/ ){
373 $suspend_user = qsearchs( 'svc_acct', { 'svcnum' => $userspec } );
374 }elsif ($userspec =~ /^\S+\@\S+$/){
375 my ($username,$domain) = split(/\@/, $userspec);
376 for my $user (qsearch( 'svc_acct', { 'username' => $username } )){
377 $suspend_user = $user if $userspec eq $user->email;
380 $suspend_user = qsearchs( 'svc_acct', { 'username' => $userspec } );
383 return $suspend_user->radius_groups if $suspend_user;
387 sub sqlradius_insert { #subroutine, not method
388 my $dbh = sqlradius_connect(shift, shift, shift);
389 my( $table, $username, %attributes ) = @_;
391 foreach my $attribute ( keys %attributes ) {
393 my $s_sth = $dbh->prepare(
394 "SELECT COUNT(*) FROM rad$table WHERE UserName = ? AND Attribute = ?"
395 ) or die $dbh->errstr;
396 $s_sth->execute( $username, $attribute ) or die $s_sth->errstr;
398 if ( $s_sth->fetchrow_arrayref->[0] ) {
400 my $u_sth = $dbh->prepare(
401 "UPDATE rad$table SET Value = ? WHERE UserName = ? AND Attribute = ?"
402 ) or die $dbh->errstr;
403 $u_sth->execute($attributes{$attribute}, $username, $attribute)
404 or die $u_sth->errstr;
408 my $i_sth = $dbh->prepare(
409 "INSERT INTO rad$table ( UserName, Attribute, op, Value ) ".
410 "VALUES ( ?, ?, ?, ? )"
411 ) or die $dbh->errstr;
415 ( $attribute eq 'Password' ? '==' : ':=' ),
416 $attributes{$attribute},
417 ) or die $i_sth->errstr;
425 sub sqlradius_usergroup_insert { #subroutine, not method
426 my $dbh = sqlradius_connect(shift, shift, shift);
427 my $username = shift;
428 my $usergroup = ( $_[0] =~ /^(rad)?usergroup/i ) ? shift : 'usergroup';
431 my $s_sth = $dbh->prepare(
432 "SELECT COUNT(*) FROM $usergroup WHERE UserName = ? AND GroupName = ?"
433 ) or die $dbh->errstr;
435 my $sth = $dbh->prepare(
436 "INSERT INTO $usergroup ( UserName, GroupName ) VALUES ( ?, ? )"
437 ) or die $dbh->errstr;
439 foreach my $group ( @groups ) {
440 $s_sth->execute( $username, $group ) or die $s_sth->errstr;
441 if ($s_sth->fetchrow_arrayref->[0]) {
442 warn localtime() . ": sqlradius_usergroup_insert attempted to reinsert " .
443 "$group for $username\n"
447 $sth->execute( $username, $group )
448 or die "can't insert into groupname table: ". $sth->errstr;
450 if ( $s_sth->{Active} ) {
451 warn "sqlradius s_sth still active; calling ->finish()";
454 if ( $sth->{Active} ) {
455 warn "sqlradius sth still active; calling ->finish()";
461 sub sqlradius_usergroup_delete { #subroutine, not method
462 my $dbh = sqlradius_connect(shift, shift, shift);
463 my $username = shift;
464 my $usergroup = ( $_[0] =~ /^(rad)?usergroup/i ) ? shift : 'usergroup';
467 my $sth = $dbh->prepare(
468 "DELETE FROM $usergroup WHERE UserName = ? AND GroupName = ?"
469 ) or die $dbh->errstr;
470 foreach my $group ( @groups ) {
471 $sth->execute( $username, $group )
472 or die "can't delete from groupname table: ". $sth->errstr;
477 sub sqlradius_rename { #subroutine, not method
478 my $dbh = sqlradius_connect(shift, shift, shift);
479 my($new_username, $old_username) = (shift, shift);
480 my $usergroup = ( $_[0] =~ /^(rad)?usergroup/i ) ? shift : 'usergroup';
481 foreach my $table (qw(radreply radcheck), $usergroup ) {
482 my $sth = $dbh->prepare("UPDATE $table SET Username = ? WHERE UserName = ?")
484 $sth->execute($new_username, $old_username)
485 or die "can't update $table: ". $sth->errstr;
490 sub sqlradius_attrib_delete { #subroutine, not method
491 my $dbh = sqlradius_connect(shift, shift, shift);
492 my( $table, $username, @attrib ) = @_;
494 foreach my $attribute ( @attrib ) {
495 my $sth = $dbh->prepare(
496 "DELETE FROM rad$table WHERE UserName = ? AND Attribute = ?" )
498 $sth->execute($username,$attribute)
499 or die "can't delete from rad$table table: ". $sth->errstr;
504 sub sqlradius_delete { #subroutine, not method
505 my $dbh = sqlradius_connect(shift, shift, shift);
506 my $username = shift;
507 my $usergroup = ( $_[0] =~ /^(rad)?usergroup/i ) ? shift : 'usergroup';
509 foreach my $table (qw( radcheck radreply), $usergroup ) {
510 my $sth = $dbh->prepare( "DELETE FROM $table WHERE UserName = ?" );
511 $sth->execute($username)
512 or die "can't delete from $table table: ". $sth->errstr;
517 sub sqlradius_connect {
518 #my($datasrc, $username, $password) = @_;
519 #DBI->connect($datasrc, $username, $password) or die $DBI::errstr;
520 DBI->connect(@_) or die $DBI::errstr;
523 sub sqlreplace_usergroups {
524 my ($self, $svcnum, $username, $jobnum, $old, $new) = @_;
526 # (sorta) false laziness with FS::svc_acct::replace
527 my @oldgroups = @$old;
528 my @newgroups = @$new;
530 foreach my $oldgroup ( @oldgroups ) {
531 if ( grep { $oldgroup eq $_ } @newgroups ) {
532 @newgroups = grep { $oldgroup ne $_ } @newgroups;
535 push @delgroups, $oldgroup;
538 my $usergroup = $self->option('usergroup') || 'usergroup';
541 my $err_or_queue = $self->sqlradius_queue( $svcnum, 'usergroup_delete',
542 $username, $usergroup, @delgroups );
544 unless ref($err_or_queue);
546 my $error = $err_or_queue->depend_insert( $jobnum );
547 return $error if $error;
552 cluck localtime(). ": queuing usergroup_insert for $svcnum ($username) ".
553 "with ". join(", ", @newgroups)
555 my $err_or_queue = $self->sqlradius_queue( $svcnum, 'usergroup_insert',
556 $username, $usergroup, @newgroups );
558 unless ref($err_or_queue);
560 my $error = $err_or_queue->depend_insert( $jobnum );
561 return $error if $error;
570 =item usage_sessions HASHREF
572 =item usage_sessions TIMESTAMP_START TIMESTAMP_END [ SVC_ACCT [ IP [ PREFIX [ SQL_SELECT ] ] ] ]
574 New-style: pass a hashref with the following keys:
578 =item stoptime_start - Lower bound for AcctStopTime, as a UNIX timestamp
580 =item stoptime_end - Upper bound for AcctStopTime, as a UNIX timestamp
582 =item open_sessions - Only show records with no AcctStopTime (typically used without stoptime_* options and with starttime_* options instead)
584 =item starttime_start - Lower bound for AcctStartTime, as a UNIX timestamp
586 =item starttime_end - Upper bound for AcctStartTime, as a UNIX timestamp
598 TIMESTAMP_START and TIMESTAMP_END are specified as UNIX timestamps; see
599 L<perlfunc/"time">. Also see L<Time::Local> and L<Date::Parse> for conversion
602 SVC_ACCT, if specified, limits the results to the specified account.
604 IP, if specified, limits the results to the specified IP address.
606 PREFIX, if specified, limits the results to records with a matching
609 #SQL_SELECT defaults to * if unspecified. It can be useful to set it to
610 #SUM(acctsessiontime) or SUM(AcctInputOctets), etc.
612 Returns an arrayref of hashrefs with the following fields:
618 =item framedipaddress
624 =item acctsessiontime
626 =item acctinputoctets
628 =item acctoutputoctets
630 =item calledstationid
636 #some false laziness w/cust_svc::seconds_since_sqlradacct
642 my($start, $end, $svc_acct, $ip, $prefix) = ( '', '', '', '', '');
646 $start = $opt->{stoptime_start};
647 $end = $opt->{stoptime_end};
648 $svc_acct = $opt->{svc_acct};
650 $prefix = $opt->{prefix};
651 $summarize = $opt->{summarize};
653 ( $start, $end ) = splice(@_, 0, 2);
654 $svc_acct = @_ ? shift : '';
655 $ip = @_ ? shift : '';
656 $prefix = @_ ? shift : '';
657 #my $select = @_ ? shift : '*';
662 return [] if $self->option('ignore_accounting');
664 my $dbh = sqlradius_connect( map $self->option($_),
665 qw( datasrc username password ) );
667 #select a unix time conversion function based on database type
668 my $str2time = str2time_sql( $dbh->{Driver}->{Name} );
671 qw( username realm framedipaddress
672 acctsessiontime acctinputoctets acctoutputoctets
675 "$str2time acctstarttime ) as acctstarttime",
676 "$str2time acctstoptime ) as acctstoptime",
679 @fields = ( 'username', 'sum(acctsessiontime) as acctsessiontime', 'sum(acctinputoctets) as acctinputoctets',
680 'sum(acctoutputoctets) as acctoutputoctets',
687 my $username = $self->export_username($svc_acct);
688 if ( $username =~ /^([^@]+)\@([^@]+)$/ ) {
689 push @where, '( UserName = ? OR ( UserName = ? AND Realm = ? ) )';
690 push @param, $username, $1, $2;
692 push @where, 'UserName = ?';
693 push @param, $username;
697 if ($self->option('process_single_realm')) {
698 push @where, 'Realm = ?';
699 push @param, $self->option('realm');
703 push @where, ' FramedIPAddress = ?';
707 if ( length($prefix) ) {
708 #assume sip: for now, else things get ugly trying to match /^\w+:$prefix/
709 push @where, " CalledStationID LIKE 'sip:$prefix\%'";
713 push @where, "$str2time AcctStopTime ) >= ?";
717 push @where, "$str2time AcctStopTime ) <= ?";
720 if ( $opt->{open_sessions} ) {
721 push @where, 'AcctStopTime IS NULL';
723 if ( $opt->{starttime_start} ) {
724 push @where, "$str2time AcctStartTime ) >= ?";
725 push @param, $opt->{starttime_start};
727 if ( $opt->{starttime_end} ) {
728 push @where, "$str2time AcctStartTime ) <= ?";
729 push @param, $opt->{starttime_end};
732 my $where = join(' AND ', @where);
733 $where = "WHERE $where" if $where;
736 $groupby = 'GROUP BY username' if $summarize;
738 my $orderby = 'ORDER BY AcctStartTime DESC';
739 $orderby = '' if $summarize;
741 my $sth = $dbh->prepare('SELECT '. join(', ', @fields).
742 " FROM radacct $where $groupby $orderby
743 ") or die $dbh->errstr;
744 $sth->execute(@param) or die $sth->errstr;
746 [ map { { %$_ } } @{ $sth->fetchall_arrayref({}) } ];
750 =item update_svc_acct
757 my $conf = new FS::Conf;
760 my $dbh = sqlradius_connect( map $self->option($_),
761 qw( datasrc username password ) );
763 my $str2time = str2time_sql( $dbh->{Driver}->{Name} );
764 my @fields = qw( radacctid username realm acctsessiontime );
769 my $sth = $dbh->prepare("
770 SELECT RadAcctId, UserName, Realm, AcctSessionTime,
771 $str2time AcctStartTime), $str2time AcctStopTime),
772 AcctInputOctets, AcctOutputOctets
774 WHERE FreesideStatus IS NULL
775 AND AcctStopTime IS NOT NULL
776 ") or die $dbh->errstr;
777 $sth->execute() or die $sth->errstr;
779 while ( my $row = $sth->fetchrow_arrayref ) {
780 my($RadAcctId, $UserName, $Realm, $AcctSessionTime, $AcctStartTime,
781 $AcctStopTime, $AcctInputOctets, $AcctOutputOctets) = @$row;
782 warn "processing record: ".
783 "$RadAcctId ($UserName\@$Realm for ${AcctSessionTime}s"
786 $UserName = lc($UserName) unless $conf->exists('username-uppercase');
788 #my %search = ( 'username' => $UserName );
791 if ( ref($self) =~ /withdomain/ ) { #well...
792 $extra_sql = " AND '$Realm' = ( SELECT domain FROM svc_domain
793 WHERE svc_domain.svcnum = svc_acct.domsvc ) ";
796 my $oldAutoCommit = $FS::UID::AutoCommit; # can't undo side effects, but at
797 local $FS::UID::AutoCommit = 0; # least we can avoid over counting
799 my $status = 'skipped';
800 my $errinfo = "for RADIUS detail RadAcctID $RadAcctId ".
801 "(UserName $UserName, Realm $Realm)";
803 if ( $self->option('process_single_realm')
804 && $self->option('realm') ne $Realm )
806 warn "WARNING: wrong realm $errinfo - skipping\n" if $DEBUG;
809 grep { qsearch( 'export_svc', { 'exportnum' => $self->exportnum,
810 'svcpart' => $_->cust_svc->svcpart, } )
813 { 'username' => $UserName },
819 warn "WARNING: no svc_acct record found $errinfo - skipping\n";
820 } elsif ( scalar(@svc_acct) > 1 ) {
821 warn "WARNING: multiple svc_acct records found $errinfo - skipping\n";
824 my $svc_acct = $svc_acct[0];
825 warn "found svc_acct ". $svc_acct->svcnum. " $errinfo\n" if $DEBUG;
827 $svc_acct->last_login($AcctStartTime);
828 $svc_acct->last_logout($AcctStopTime);
830 my $session_time = $AcctStopTime;
831 $session_time = $AcctStartTime if $self->option('ignore_long_sessions');
833 my $cust_pkg = $svc_acct->cust_svc->cust_pkg;
834 if ( $cust_pkg && $session_time < ( $cust_pkg->last_bill
835 || $cust_pkg->setup ) ) {
836 $status = 'skipped (too old)';
839 push @st, _try_decrement($svc_acct, 'seconds', $AcctSessionTime);
840 push @st, _try_decrement($svc_acct, 'upbytes', $AcctInputOctets);
841 push @st, _try_decrement($svc_acct, 'downbytes', $AcctOutputOctets);
842 push @st, _try_decrement($svc_acct, 'totalbytes', $AcctInputOctets
843 + $AcctOutputOctets);
844 $status=join(' ', @st);
849 warn "setting FreesideStatus to $status $errinfo\n" if $DEBUG;
850 my $psth = $dbh->prepare("UPDATE radacct
851 SET FreesideStatus = ?
853 ) or die $dbh->errstr;
854 $psth->execute($status, $RadAcctId) or die $psth->errstr;
856 $fdbh->commit or die $fdbh->errstr if $oldAutoCommit;
863 my ($svc_acct, $column, $amount) = @_;
864 if ( $svc_acct->$column !~ /^$/ ) {
865 warn " svc_acct.$column found (". $svc_acct->$column.
868 my $method = 'decrement_' . $column;
869 my $error = $svc_acct->$method($amount);
870 die $error if $error;
873 warn " no existing $column value for svc_acct - skipping\n" if $DEBUG;
878 =item export_nas_insert NAS
880 =item export_nas_delete NAS
882 =item export_nas_replace NEW_NAS OLD_NAS
884 Update the NAS table (allowed RADIUS clients) on the attached RADIUS
885 server. Currently requires the table to be named 'nas' and to follow
886 the stock schema (/etc/freeradius/nas.sql).
890 sub export_nas_insert { shift->export_nas_action('insert', @_); }
891 sub export_nas_delete { shift->export_nas_action('delete', @_); }
892 sub export_nas_replace { shift->export_nas_action('replace', @_); }
894 sub export_nas_action {
896 my ($action, $new, $old) = @_;
897 # find the NAS in the target table by its name
898 my $nasname = ($action eq 'replace') ? $old->nasname : $new->nasname;
899 my $nasnum = $new->nasnum;
901 my $err_or_queue = $self->sqlradius_queue('', "nas_$action",
905 return $err_or_queue unless ref $err_or_queue;
909 sub sqlradius_nas_insert {
910 my $dbh = sqlradius_connect(shift, shift, shift);
912 my $nas = qsearchs('nas', { nasnum => $opt{'nasnum'} })
913 or die "nasnum ".$opt{'nasnum'}.' not found';
914 # insert actual NULLs where FS::Record has translated to empty strings
915 my @values = map { length($nas->$_) ? $nas->$_ : undef }
916 qw( nasname shortname type secret server community description );
917 my $sth = $dbh->prepare('INSERT INTO nas
918 (nasname, shortname, type, secret, server, community, description)
919 VALUES (?, ?, ?, ?, ?, ?, ?)');
920 $sth->execute(@values) or die $dbh->errstr;
923 sub sqlradius_nas_delete {
924 my $dbh = sqlradius_connect(shift, shift, shift);
926 my $sth = $dbh->prepare('DELETE FROM nas WHERE nasname = ?');
927 $sth->execute($opt{'nasname'}) or die $dbh->errstr;
930 sub sqlradius_nas_replace {
931 my $dbh = sqlradius_connect(shift, shift, shift);
933 my $nas = qsearchs('nas', { nasnum => $opt{'nasnum'} })
934 or die "nasnum ".$opt{'nasnum'}.' not found';
935 my @values = map {$nas->$_}
936 qw( nasname shortname type secret server community description );
937 my $sth = $dbh->prepare('UPDATE nas SET
938 nasname = ?, shortname = ?, type = ?, secret = ?,
939 server = ?, community = ?, description = ?
941 $sth->execute(@values, $opt{'nasname'}) or die $dbh->errstr;
951 #don't just look for ->can('usage_sessions'), we're sqlradius-specific
952 # (radiator is supposed to be setup with a radacct table)
953 #i suppose it would be more slick to look for things that inherit from us..
955 my @part_export = ();
956 push @part_export, qsearch('part_export', { 'exporttype' => $_ } )
957 foreach qw( sqlradius sqlradius_withdomain radiator phone_sqlradius );
961 sub all_sqlradius_withaccounting {
963 grep { ! $_->option('ignore_accounting') } $class->all_sqlradius;