FEX.pm:
-------
sub get_cops_web
{
my ($file,$user,$password,$logfile,$debug) = @_;
$user=~s/\s*\r*\n*//g;
$password=~s/\s*\r*\n*//g;
$file=~s/\s*\r*\n*//g;
$file=~s/\.\.//g;
$file=~s/^([a-z]+:[\/]+)*(\d+\.\d+\.\d+\.\d+)*\///g;
if ($file!~/^[a-z0-9%?&=\/_-]+\.[a-z0-9%?&=_-]+/i) { print "\nInvalid file!\n"; return 0; }
my $path="/srv/www/htdocs/";
$file=$path.$file;
my $local_tmp="/opt/vpn/CoCA/TEMP/";
my $logfile="/opt/vpn/logs/copsweb_log_".$user;
my $screen_log=0;
$|=1;
my $telnet;
$telnet=&ssh_nipc1($telnet,$user,$password,'',$logfile,$screen_log,$debug);
$telnet->raw_pty(1);
$telnet->debug($debug);
$telnet->log_file($logfile);
$telnet->log_user(1);
$telnet->log_stdout($screen_log);
print $telnet "export PS1=\"\"\r";
$telnet->expect(5,'-re',"\r*\n*nipc1:~> \s*\r*\n*");
my $good="EXPECT_EOF";
my $bad = 'o such file or directory|ould not open';
my $command="/usr/bin/php /srv/www/htdocs/cops/coca_overlay.php $file && echo \"EXPECT_EOF\"\r";
print $telnet $command;
my ($match_num,$error,$match,$before,$after) = $telnet->expect(40,'-re',$good,'-re',$bad);
print $telnet "export PS1=\"\\u@\\h:\\w>\"\r";
$telnet->expect(10,'-re',"\r*\n*nipc1:~>\s*\r*\n*");
&close_nipc1($telnet,$user,$password,'',$logfile,$screen_log,$debug);
my $sequence;
if ($match=~/$bad/i) { $before="Could not read file $file in FEX.pm\r\n\r\n$match"; $sequence=0; }
else { $sequence=&create_random_sequence(32); }
open (DESTINATION, ">", $local_tmp."copsweb_".$sequence) or die "Error writing file copsweb_".$sequence."! \n$!\n";
binmode(DESTINATION);
print DESTINATION $before;
close (DESTINATION);
return $sequence;
}
sub ssh_unix_level1
{
my ($telnet,$host,$hostname,$user,$password,$ra_meldungen,$logfile,$screen_log,$debug) = @_;
$user=~s/\s*\r*\n*//g;
$password=~s/\s*\r*\n*//g;
if (!$hostname)
{
if ($host eq "1.1.1.1") { $hostname="hop1"; }
elsif ($host eq "1.1.1.2") { $hostname="hop2"; }
elsif ($host eq "1.1.1.3") { $hostname="hop3"; }
}
my $lockfile="/opt/vpn/locks/".(lc($hostname))."_anwahl_".$user;
my $logfile="/opt/vpn/logs/".(lc($hostname))."_log_".$user;
open LOCKDATEI, "> $lockfile" or die "$! FEX::hop1_anwahl kann Datei $lockfile nicht oeffnen\n";
flock(LOCKDATEI,LOCK_EX);
print LOCKDATEI (lc($hostname))."\n";
$|=1;
### Expect Objekt erzeugen, wenn direkte Anwahl, ansonsten per
### telnet vom bereits geoeffneten Objekt loslegen.
my $ref_tel=ref($telnet);
if ($ref_tel ne "Expect")
{
$Expect::Log_Stdout= $screen_log;
$telnet=new Expect;
$telnet->raw_pty(1);
$telnet->slave->stty(qw(raw -echo));
$telnet->spawn("/usr/bin/ssh ".$user."@".$host) or die "konnte SSH nicht starten $!\n";
$telnet->debug($debug);
$telnet->log_file($logfile);
$telnet->log_user(1);
$telnet->log_stdout($screen_log);
}
else
{ print $telnet "ssh ".$user."@".$host."\n"; }
my $good1 = 'Are you sure you want to continue connecting (yes\/no)?';
my $good2 = 'Password:';
my $good = $good1."|".$good2;
my $bad = 'Unable to connect|connection refused|ad passwords|onnection closed|Unknown host|onnection timed|not known';
my ($match_num,$error,$match,$before,$after) = $telnet->expect(5,'-re',$good,'-re',$bad);
if ($match=~/$bad/i) { $telnet->hard_close(); }
elsif ($match=~/$good1/i)
{
print $telnet "yes\r";
($match_num,$error,$match,$before,$after) = $telnet->expect(5,'-re',$good2,'-re',$bad);
}
if ($match=~/$good2/i)
{
print $telnet $password."\r\n";
$bad.="|Password:";
$good="\@".$hostname.":~>";
($match_num,$error,$match,$before,$after) = $telnet->expect(5,'-re',$good,'-re',$bad);
if ($match=~/$bad/i) { $telnet->hard_close(); }
elsif ($match=~/$good/i)
{
$telnet->log_user(1);
flock(LOCKDATEI,LOCK_UN);
close LOCKDATEI;
return $telnet;
}
}
else { $telnet->hard_close(); }
flock(LOCKDATEI,LOCK_UN);
close LOCKDATEI;
print "\n\nProblem: Einloggen auf ".(lc($hostname))." war nicht erfolgreich !!!\n\n";
return 0;
}
sub close_unix_level1
{
my ($telnet,$host,$user,$password,$ra_meldungen,$logfile,$screen_log,$debug)=@_;
$telnet->raw_pty(1);
$telnet->debug($debug);
$telnet->log_file($logfile);
$telnet->log_user(1);
$telnet->log_stdout($screen_log);
$|=1;
my $hostname;
if ($host eq "1.1.1.1") { $hostname="hop1"; }
elsif ($host eq "1.1.1.2") { $hostname="hop2"; }
elsif ($host eq "1.1.1.3") { $hostname="hop3"; }
elsif ($host=~/[a-z]+/i) { $hostname=$host; }
my $logfile="/opt/vpn/locks/".$hostname."_log_".$user;
my $ref_tel=ref($telnet);
if ($ref_tel eq "Expect")
{
print $telnet "exit\r";
my $good = "closed";
my $bad = 'not found|unknown';
my ($match_num,$error,$match,$before,$after) = $telnet->expect(20,'-re',$good,'-re',$bad);
if ($match=~/$bad/i)
{
print $telnet "quit\r";
$good = "closed";
$bad = 'not found|unknown';
($match_num,$error,$match,$before,$after) = $telnet->expect(20,'-re',$good,'-re',$bad);
}
$telnet->soft_close();
$telnet->hard_close();
}
return 1;
}
sub ssh_nipc1
{
my ($telnet,$user,$password,$ra_meldungen,$logfile,$screen_log,$debug) = @_;
my $lockfile = "/opt/vpn/locks/nipc1_anwahl_$user";
my $logfile = "/opt/vpn/logs/nipc1_log_$user";
my $host="1.1.1.4";
open LOCKDATEI, "> $lockfile" or die "$! FEX::nipc1_anwahl kann Datei $lockfile nicht oeffnen\n";
flock(LOCKDATEI,LOCK_EX);
print LOCKDATEI "nipc1\n";
$|=1;
#Trying Unix hosts (official gates)
my $count=0; my $count2=0;
my @hop_ips= ("1.1.1.1", "1.1.1.2", "1.1.1.3");
my @hop_hostnames=("hop1", "hop2", "hop3");
for ($count=0;$count<99;$count++)
{
if (!$hop_ips[$count] || !$hop_hostnames[$count]) { $count=99; last; }
for ($count2=0;$count2<3;$count2++)
{
$telnet=&ssh_unix_level1($telnet,$hop_ips[$count],$hop_hostnames[$count],$user,$password,$ra_meldungen,$logfile,$screen_log,$debug);
if (ref($telnet) eq "Expect") { $FEX::pre_logfile="/opt/vpn/logs/".(lc($hop_hostnames[$count]))."_log_".$user; $FEX::pre_host=lc($hop_hostnames[$count]); $count2=3; $count=99; }
}
}
my $ref_tel=ref($telnet);
if ($ref_tel ne "Expect") { return 0; }
else
{
$user="user";
$password="password";
$telnet->raw_pty(1);
$telnet->debug($debug);
$telnet->log_file($logfile);
$telnet->log_user(1);
$telnet->log_stdout($screen_log);
print $telnet "ssh ".$user."@".$host."\n";
}
my $good1 = 'Are you sure you want to continue connecting (yes\/no)?';
my $good2 = 'Password:';
my $good = $good1."|".$good2;
my $bad = 'Unable to connect|connection refused|ad passwords|onnection closed|Unknown host|onnection timed|not known';
my ($match_num,$error,$match,$before,$after) = $telnet->expect(10,'-re',$good,'-re',$bad);
if ($match=~/$bad/i) { $telnet->hard_close(); }
elsif ($match=~/$good1/i)
{
print $telnet "yes\r";
($match_num,$error,$match,$before,$after) = $telnet->expect(10,'-re',$good2,'-re',$bad);
}
if ($match=~/$good2/i)
{
print $telnet $password."\r\n";
$bad.="|Password:";
$good="\@nipc1:~>";
($match_num,$error,$match,$before,$after) = $telnet->expect(10,'-re',$good,'-re',$bad);
if ($match=~/$bad/i) { $telnet->hard_close(); }
elsif ($match=~/$good/i)
{
$telnet->log_user(1);
flock(LOCKDATEI,LOCK_UN);
close LOCKDATEI;
return $telnet;
}
}
else { $telnet->hard_close(); }
flock(LOCKDATEI,LOCK_UN);
close LOCKDATEI;
print "\n\nProblem: Einloggen auf nipc1 war nicht erfolgreich !!!\n\n";
return 0;
}
sub close_nipc1
{
my ($telnet,$user,$password,$ra_meldungen,$logfile,$screen_log,$debug)=@_;
$telnet->raw_pty(1);
$telnet->debug($debug);
$telnet->log_file($logfile);
$telnet->log_user(1);
$telnet->log_stdout($screen_log);
$|=1;
my $logfile = "/opt/vpn/logs/nipc1_log_$user";
my $ref_tel = ref($telnet);
if ($ref_tel eq "Expect")
{
print $telnet "exit\r";
my $good = "closed";
my $bad = 'not found|unknown';
my ($match_num,$error,$match,$before,$after) = $telnet->expect(20,'-re',$good,'-re',$bad);
if ($match=~/$bad/i)
{
print $telnet "quit\r";
$good = "closed";
$bad = 'not found|unknown';
($match_num,$error,$match,$before,$after) = $telnet->expect(20,'-re',$good,'-re',$bad);
}
$telnet->log_file($FEX::pre_logfile);
&close_unix_level1($telnet,$FEX::pre_host,$user,$password,$ra_meldungen,$logfile,$screen_log,$debug);
$telnet->soft_close();
$telnet->hard_close();
}
return 1;
}