|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
package sshhelp; |
|
|
|
|
|
use strict; |
|
|
use warnings; |
|
|
|
|
|
BEGIN { |
|
|
use base qw(Exporter); |
|
|
|
|
|
our @EXPORT_OK = qw( |
|
|
$sshdexe |
|
|
$sshexe |
|
|
$sftpsrvexe |
|
|
$sftpexe |
|
|
$sshkeygenexe |
|
|
$sshdconfig |
|
|
$sshconfig |
|
|
$sftpconfig |
|
|
$knownhosts |
|
|
$sshdlog |
|
|
$sshlog |
|
|
$sftplog |
|
|
$sftpcmds |
|
|
$hstprvkeyf |
|
|
$hstpubkeyf |
|
|
$hstpubmd5f |
|
|
$hstpubsha256f |
|
|
$cliprvkeyf |
|
|
$clipubkeyf |
|
|
display_sshdconfig |
|
|
display_sshconfig |
|
|
display_sftpconfig |
|
|
display_sshdlog |
|
|
display_sshlog |
|
|
display_sftplog |
|
|
dump_array |
|
|
find_sshd |
|
|
find_ssh |
|
|
find_sftpsrv |
|
|
find_sftp |
|
|
find_sshkeygen |
|
|
find_httptlssrv |
|
|
sshversioninfo |
|
|
); |
|
|
} |
|
|
|
|
|
use File::Spec; |
|
|
|
|
|
use pathhelp qw( |
|
|
exe_ext |
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
our $sshdexe = 'sshd' .exe_ext('SSH'); |
|
|
our $sshexe = 'ssh' .exe_ext('SSH'); |
|
|
our $sftpsrvexe = 'sftp-server' .exe_ext('SSH'); |
|
|
our $sftpexe = 'sftp' .exe_ext('SSH'); |
|
|
our $sshkeygenexe = 'ssh-keygen' .exe_ext('SSH'); |
|
|
our $httptlssrvexe = 'gnutls-serv' .exe_ext('SSH'); |
|
|
our $sshdconfig = 'curl_sshd_config'; |
|
|
our $sshconfig = 'curl_ssh_config'; |
|
|
our $sftpconfig = 'curl_sftp_config'; |
|
|
our $sshdlog = undef; |
|
|
our $sshlog = undef; |
|
|
our $sftplog = undef; |
|
|
our $sftpcmds = 'curl_sftp_cmds'; |
|
|
our $knownhosts = 'curl_client_knownhosts'; |
|
|
our $hstprvkeyf = 'curl_host_rsa_key'; |
|
|
our $hstpubkeyf = 'curl_host_rsa_key.pub'; |
|
|
our $hstpubmd5f = 'curl_host_rsa_key.pub_md5'; |
|
|
our $hstpubsha256f = 'curl_host_rsa_key.pub_sha256'; |
|
|
our $cliprvkeyf = 'curl_client_key'; |
|
|
our $clipubkeyf = 'curl_client_key.pub'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
our @sftppath = qw( |
|
|
/usr/lib/openssh |
|
|
/usr/libexec/openssh |
|
|
/usr/libexec |
|
|
/usr/local/libexec |
|
|
/opt/local/libexec |
|
|
/usr/lib/ssh |
|
|
/usr/libexec/ssh |
|
|
/usr/sbin |
|
|
/usr/lib |
|
|
/usr/lib/ssh/openssh |
|
|
/usr/lib64/ssh |
|
|
/usr/lib64/misc |
|
|
/usr/lib/misc |
|
|
/usr/local/sbin |
|
|
/usr/freeware/bin |
|
|
/usr/freeware/sbin |
|
|
/usr/freeware/libexec |
|
|
/opt/ssh/sbin |
|
|
/opt/ssh/libexec |
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
our @httptlssrvpath = qw( |
|
|
/usr/sbin |
|
|
/usr/libexec |
|
|
/usr/lib |
|
|
/usr/lib/misc |
|
|
/usr/lib64/misc |
|
|
/usr/local/bin |
|
|
/usr/local/sbin |
|
|
/usr/local/libexec |
|
|
/opt/local/bin |
|
|
/opt/local/sbin |
|
|
/opt/local/libexec |
|
|
/usr/freeware/bin |
|
|
/usr/freeware/sbin |
|
|
/usr/freeware/libexec |
|
|
/opt/gnutls/bin |
|
|
/opt/gnutls/sbin |
|
|
/opt/gnutls/libexec |
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sub dump_array { |
|
|
my ($filename, @arr) = @_; |
|
|
my $error; |
|
|
|
|
|
if(!$filename) { |
|
|
$error = 'Error: Missing argument 1 for dump_array()'; |
|
|
} |
|
|
elsif(open(my $textfh, ">", $filename)) { |
|
|
foreach my $line (@arr) { |
|
|
$line .= "\n" if($line !~ /\n$/); |
|
|
print $textfh $line; |
|
|
} |
|
|
if(!close($textfh)) { |
|
|
$error = "Error: cannot close file $filename"; |
|
|
} |
|
|
} |
|
|
else { |
|
|
$error = "Error: cannot write file $filename"; |
|
|
} |
|
|
return $error; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sub display_file { |
|
|
my $filename = $_[0]; |
|
|
print "=== Start of file $filename\n"; |
|
|
if(open(my $displayfh, "<", "$filename")) { |
|
|
while(my $line = <$displayfh>) { |
|
|
print "$line"; |
|
|
} |
|
|
close $displayfh; |
|
|
} |
|
|
print "=== End of file $filename\n"; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sub display_sshdconfig { |
|
|
display_file($sshdconfig); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sub display_sshconfig { |
|
|
display_file($sshconfig); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sub display_sftpconfig { |
|
|
display_file($sftpconfig); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sub display_sshdlog { |
|
|
die "error: \$sshdlog uninitialized" if(not defined $sshdlog); |
|
|
display_file($sshdlog); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sub display_sshlog { |
|
|
die "error: \$sshlog uninitialized" if(not defined $sshlog); |
|
|
display_file($sshlog); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sub display_sftplog { |
|
|
die "error: \$sftplog uninitialized" if(not defined $sftplog); |
|
|
display_file($sftplog); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sub find_file { |
|
|
my $fn = $_[0]; |
|
|
shift; |
|
|
my @path = @_; |
|
|
foreach (@path) { |
|
|
my $file = File::Spec->catfile($_, $fn); |
|
|
if(-e $file && ! -d $file) { |
|
|
return $file; |
|
|
} |
|
|
} |
|
|
return ""; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sub find_exe_file { |
|
|
my $fn = $_[0]; |
|
|
shift; |
|
|
my @path = @_; |
|
|
my $xext = exe_ext('SSH'); |
|
|
foreach (@path) { |
|
|
my $file = File::Spec->catfile($_, $fn); |
|
|
if(-e $file && ! -d $file) { |
|
|
return $file if(-x $file); |
|
|
return $file if(($xext) && (lc($file) =~ /\Q$xext\E$/)); |
|
|
} |
|
|
} |
|
|
return ""; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sub find_file_spath { |
|
|
my $filename = $_[0]; |
|
|
my @spath; |
|
|
push(@spath, File::Spec->path()); |
|
|
push(@spath, @sftppath); |
|
|
return find_file($filename, @spath); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sub find_exe_file_hpath { |
|
|
my $filename = $_[0]; |
|
|
my @hpath; |
|
|
push(@hpath, File::Spec->path()); |
|
|
push(@hpath, @httptlssrvpath); |
|
|
return find_exe_file($filename, @hpath); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sub find_sshd { |
|
|
return find_file_spath($sshdexe); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sub find_ssh { |
|
|
return find_file_spath($sshexe); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sub find_sftpsrv { |
|
|
return find_file_spath($sftpsrvexe); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sub find_sftp { |
|
|
return find_file_spath($sftpexe); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sub find_sshkeygen { |
|
|
return find_file_spath($sshkeygenexe); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sub find_httptlssrv { |
|
|
my $p = find_exe_file_hpath($httptlssrvexe); |
|
|
if($p) { |
|
|
my @o = `"$p" -l`; |
|
|
my $found; |
|
|
for(@o) { |
|
|
if(/Key exchange: SRP/) { |
|
|
$found = 1; |
|
|
last; |
|
|
} |
|
|
} |
|
|
return $p if($found); |
|
|
} |
|
|
return ""; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sub sshversioninfo { |
|
|
my $sshbin = $_[0]; |
|
|
my $major; |
|
|
my $minor; |
|
|
my $patch; |
|
|
my $sshid; |
|
|
my $versnum; |
|
|
my $versstr; |
|
|
my $error; |
|
|
|
|
|
if(!$sshbin) { |
|
|
$error = 'Error: Missing argument 1 for sshversioninfo()'; |
|
|
} |
|
|
elsif(! -x $sshbin) { |
|
|
$error = "Error: cannot read or execute $sshbin"; |
|
|
} |
|
|
else { |
|
|
my $cmd = ($sshbin =~ /$sshdexe$/) ? "\"$sshbin\" -?" : "\"$sshbin\" -V"; |
|
|
$error = "$cmd\n"; |
|
|
foreach my $tmpstr (qx($cmd 2>&1)) { |
|
|
if($tmpstr =~ /OpenSSH[_-](\d+)\.(\d+)(\.(\d+))*/i) { |
|
|
$major = $1; |
|
|
$minor = $2; |
|
|
$patch = $4?$4:0; |
|
|
$sshid = 'OpenSSH'; |
|
|
$versnum = (100*$major) + (10*$minor) + $patch; |
|
|
$versstr = "$sshid $major.$minor.$patch"; |
|
|
$error = undef; |
|
|
last; |
|
|
} |
|
|
if($tmpstr =~ /OpenSSH[_-]for[_-]Windows[_-](\d+)\.(\d+)(\.(\d+))*/i) { |
|
|
$major = $1; |
|
|
$minor = $2; |
|
|
$patch = $4?$4:0; |
|
|
$sshid = 'OpenSSH-Windows'; |
|
|
$versnum = (100*$major) + (10*$minor) + $patch; |
|
|
$versstr = "$sshid $major.$minor.$patch"; |
|
|
$error = undef; |
|
|
last; |
|
|
} |
|
|
if($tmpstr =~ /Sun[_-]SSH[_-](\d+)\.(\d+)(\.(\d+))*/i) { |
|
|
$major = $1; |
|
|
$minor = $2; |
|
|
$patch = $4?$4:0; |
|
|
$sshid = 'SunSSH'; |
|
|
$versnum = (100*$major) + (10*$minor) + $patch; |
|
|
$versstr = "$sshid $major.$minor.$patch"; |
|
|
$error = undef; |
|
|
last; |
|
|
} |
|
|
$error .= $tmpstr; |
|
|
} |
|
|
chomp $error if($error); |
|
|
} |
|
|
return ($sshid, $versnum, $versstr, $error); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1; |
|
|
|