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;
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 } );
}
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";
}
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,
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 {