X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=blobdiff_plain;f=fs_sesmon%2Ffs_session_server;h=00229f8dc9f1db7349f826e1f0514c2f380fb5e9;hp=46e53d118ebd6baa50eed99f4706c9c8ee05e1e4;hb=c70c1b17103b5bd20adb3c8a7c23ca2238fca73d;hpb=7f07089722bfcabe3bf42619bb2bdb81fd8d44e1 diff --git a/fs_sesmon/fs_session_server b/fs_sesmon/fs_session_server index 46e53d118..00229f8dc 100644 --- a/fs_sesmon/fs_session_server +++ b/fs_sesmon/fs_session_server @@ -6,9 +6,9 @@ use strict; use vars qw( $opt $Debug ); use IO::Handle; -use Net::SSH qw(sshopen3) -use FS::UID qw(adminsuidsetup); -use FS::Record qw( qsearch qsearchs ); +use Net::SSH qw(sshopen2); +use FS::UID qw(adminsuidsetup dbh); +use FS::Record qw( qsearchs ); #qsearch ); #use FS::cust_main_county; #use FS::cust_main; use FS::session; @@ -31,25 +31,28 @@ while (1) { my($reader, $writer) = (new IO::Handle, new IO::Handle); $writer->autoflush(1); warn "$me Connecting to $machine\n" if $Debug; - sshopen2($machine,$reader,$writer,$fs_signupd); + sshopen2($machine,$reader,$writer,$fs_sessiond); warn "$me Entering main loop\n" if $Debug; while (1) { warn "$me Reading (waiting for) data\n" if $Debug; - my $command = scalar(<$reader)); + my $command = scalar(<$reader>); + chomp $command; #DoS protection here too, to protect against a compromised client? *sigh* - while ( ( my $key = scalar(<$reader>) ) != "END\n" ) { + my %hash; + while ( ( my $key = scalar(<$reader>) ) ne "END\n" ) { chomp $key; chomp( $hash{$key} = scalar(<$reader>) ); } if ( $command eq 'login' ) { - $error = &login(\%hash); + my $error = &login(\%hash); print $writer "$error\n"; - } elsif ( $command eq 'logoff' ) { - $error = &logoff(\%hash); + } elsif ( $command eq 'logout' ) { + my $error = &logout(\%hash); print $writer "$error\n"; } elsif ( $command eq 'portnum' ) { + my $port; if ( exists $hash{'ip'} ) { $hash{'ip'} =~ /^([\d\.]+)$/ or $1='nomatch'; $port = qsearchs('port', { 'ip' => $1 } ); @@ -60,13 +63,13 @@ while (1) { } print $writer ( $port ? $port->portnum : '' ), "\n"; } else { - warn "$me WARNING: unrecognized command"; + warn "$me WARNING: unrecognized command: $command"; } } #won't ever reach without code above to throw out of loop, but... close $writer; close $reader; - warn "connection to $machine lost!\n" + warn "connection to $machine lost!\n"; sleep 5; warn "reconnecting...\n"; } @@ -75,11 +78,12 @@ sub login { my $href = shift; $href->{'username'} =~ /^([a-z0-9_\-\.]+)$/ or return "Illegal username"; my $username = $1; - $svc_acct = qsearchs('svc_acct', { 'username' => $username } ) + my $svc_acct = qsearchs('svc_acct', { 'username' => $username } ) or return "Unknown user"; return "Incorrect password" - if defined($href->{'password'}) + if exists($href->{'password'}) && $href->{'password'} ne $svc_acct->_password; + return "Time limit exceeded" unless $svc_acct->seconds; my $session = new FS::session { 'portnum' => $href->{'portnum'}, 'svcnum' => $svc_acct->svcnum, @@ -92,19 +96,42 @@ sub logout { my $href = shift; $href->{'username'} =~ /^([a-z0-9_\-\.]+)$/ or return "Illegal username"; my $username = $1; - $svc_acct = qsearchs('svc_acct', { 'username' => $username } ) + local $FS::UID::AutoCommit = 0; + my $dbh = dbh; + my $svc_acct = + qsearchs('svc_acct', { 'username' => $username }, '', 'FOR UPDATE' ) or return "Unknown user"; return "Incorrect password" - if defined($href->{'password'}) + if exists($href->{'password'}) && $href->{'password'} ne $svc_acct->_password; - my $session = qsearchs FS::session { - 'portnum' => $href->{'portnum'}, - 'svcnum' => $svc_acct->svcnum, - 'logoff' => '', - }; - return "No currently open sessios found for that user/port!" unless $session; - my $nsession = new FS::session ( { $old->hash } ); - $nsession->replace($session); + my $session = qsearchs( 'session', { + 'portnum' => $href->{'portnum'}, + 'svcnum' => $svc_acct->svcnum, + 'logout' => '', + }, + '', 'FOR UPDATE' + ); + unless ( $session ) { + $dbh->rollback; + return "No currently open sessions found for that user/port!"; + } + my $nsession = new FS::session ( { $session->hash } ); + warn "$nsession replacing $session"; + my $error = $nsession->replace($session); + if ( $error ) { + $dbh->rollback; + return "can't logout: $error"; + } + my $time = $nsession->logout - $nsession->login; + my $new_svc_acct = new FS::svc_acct ( { $svc_acct->hash } ); + my $seconds = $new_svc_acct->seconds; + $seconds -= $time; + $seconds = 0 if $seconds < 0; + $new_svc_acct->seconds( $seconds ); + $error = $new_svc_acct->replace( $svc_acct ); + warn "can't debit time: $error\n"; #don't want to rollback, though + $dbh->commit or die $dbh->errstr; + '' } sub usage {