2008-01-04 04:48:22 +08:00
|
|
|
#***************************************************************************
|
|
|
|
# _ _ ____ _
|
|
|
|
# Project ___| | | | _ \| |
|
|
|
|
# / __| | | | |_) | |
|
|
|
|
# | (__| |_| | _ <| |___
|
|
|
|
# \___|\___/|_| \_\_____|
|
|
|
|
#
|
2023-01-02 20:51:48 +08:00
|
|
|
# Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
|
2008-01-04 04:48:22 +08:00
|
|
|
#
|
|
|
|
# This software is licensed as described in the file COPYING, which
|
|
|
|
# you should have received as part of this distribution. The terms
|
2020-11-04 21:02:01 +08:00
|
|
|
# are also available at https://curl.se/docs/copyright.html.
|
2008-01-04 04:48:22 +08:00
|
|
|
#
|
|
|
|
# You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
# copies of the Software, and permit persons to whom the Software is
|
|
|
|
# furnished to do so, under the terms of the COPYING file.
|
|
|
|
#
|
|
|
|
# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
# KIND, either express or implied.
|
|
|
|
#
|
2011-10-07 02:26:42 +08:00
|
|
|
# SPDX-License-Identifier: curl
|
2022-05-17 17:16:50 +08:00
|
|
|
#
|
2008-01-04 04:48:22 +08:00
|
|
|
#***************************************************************************
|
|
|
|
|
|
|
|
package sshhelp;
|
|
|
|
|
|
|
|
use strict;
|
2010-01-16 02:55:01 +08:00
|
|
|
use warnings;
|
2008-01-04 04:48:22 +08:00
|
|
|
|
2023-04-06 06:29:00 +08:00
|
|
|
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
|
2008-01-04 04:48:22 +08:00
|
|
|
);
|
2023-04-06 06:29:00 +08:00
|
|
|
}
|
2008-01-04 04:48:22 +08:00
|
|
|
|
2023-04-06 06:29:00 +08:00
|
|
|
use File::Spec;
|
2008-01-04 04:48:22 +08:00
|
|
|
|
2023-04-06 06:29:00 +08:00
|
|
|
use pathhelp qw(
|
2010-01-21 22:26:32 +08:00
|
|
|
exe_ext
|
2008-01-04 04:48:22 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
#***************************************************************************
|
|
|
|
# Global variables initialization
|
|
|
|
#
|
2023-04-06 06:29:00 +08:00
|
|
|
our $sshdexe = 'sshd' .exe_ext('SSH'); # base name and ext of ssh daemon
|
|
|
|
our $sshexe = 'ssh' .exe_ext('SSH'); # base name and ext of ssh client
|
|
|
|
our $sftpsrvexe = 'sftp-server' .exe_ext('SSH'); # base name and ext of sftp-server
|
|
|
|
our $sftpexe = 'sftp' .exe_ext('SSH'); # base name and ext of sftp client
|
|
|
|
our $sshkeygenexe = 'ssh-keygen' .exe_ext('SSH'); # base name and ext of ssh-keygen
|
|
|
|
our $httptlssrvexe = 'gnutls-serv' .exe_ext('SSH'); # base name and ext of gnutls-serv
|
|
|
|
our $sshdconfig = 'curl_sshd_config'; # ssh daemon config file
|
|
|
|
our $sshconfig = 'curl_ssh_config'; # ssh client config file
|
|
|
|
our $sftpconfig = 'curl_sftp_config'; # sftp client config file
|
|
|
|
our $sshdlog = undef; # ssh daemon log file
|
|
|
|
our $sshlog = undef; # ssh client log file
|
|
|
|
our $sftplog = undef; # sftp client log file
|
|
|
|
our $sftpcmds = 'curl_sftp_cmds'; # sftp client commands batch file
|
|
|
|
our $knownhosts = 'curl_client_knownhosts'; # ssh knownhosts file
|
|
|
|
our $hstprvkeyf = 'curl_host_rsa_key'; # host private key file
|
|
|
|
our $hstpubkeyf = 'curl_host_rsa_key.pub'; # host public key file
|
|
|
|
our $hstpubmd5f = 'curl_host_rsa_key.pub_md5'; # md5 hash of host public key
|
|
|
|
our $hstpubsha256f = 'curl_host_rsa_key.pub_sha256'; # sha256 hash of host public key
|
|
|
|
our $cliprvkeyf = 'curl_client_key'; # client private key file
|
|
|
|
our $clipubkeyf = 'curl_client_key.pub'; # client public key file
|
2008-01-04 04:48:22 +08:00
|
|
|
|
|
|
|
|
|
|
|
#***************************************************************************
|
2011-10-07 02:26:42 +08:00
|
|
|
# Absolute paths where to look for sftp-server plugin, when not in PATH
|
2008-01-04 04:48:22 +08:00
|
|
|
#
|
2023-04-06 06:29:00 +08:00
|
|
|
our @sftppath = qw(
|
2008-01-04 04:48:22 +08:00
|
|
|
/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
|
2008-01-09 08:58:48 +08:00
|
|
|
/usr/freeware/sbin
|
|
|
|
/usr/freeware/libexec
|
2008-01-04 04:48:22 +08:00
|
|
|
/opt/ssh/sbin
|
|
|
|
/opt/ssh/libexec
|
|
|
|
);
|
|
|
|
|
|
|
|
|
2011-10-07 02:26:42 +08:00
|
|
|
#***************************************************************************
|
|
|
|
# Absolute paths where to look for httptlssrv (gnutls-serv), when not in PATH
|
|
|
|
#
|
2023-04-06 06:29:00 +08:00
|
|
|
our @httptlssrvpath = qw(
|
2011-10-07 02:26:42 +08:00
|
|
|
/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
|
|
|
|
);
|
|
|
|
|
|
|
|
|
2008-01-04 04:48:22 +08:00
|
|
|
#***************************************************************************
|
|
|
|
# Create or overwrite the given file with lines from an array of strings
|
|
|
|
#
|
|
|
|
sub dump_array {
|
|
|
|
my ($filename, @arr) = @_;
|
|
|
|
my $error;
|
|
|
|
|
|
|
|
if(!$filename) {
|
|
|
|
$error = 'Error: Missing argument 1 for dump_array()';
|
|
|
|
}
|
2023-03-31 12:32:17 +08:00
|
|
|
elsif(open(my $textfh, ">", $filename)) {
|
2008-01-04 04:48:22 +08:00
|
|
|
foreach my $line (@arr) {
|
2023-03-28 10:58:00 +08:00
|
|
|
$line .= "\n" if($line !~ /\n$/);
|
2023-03-29 04:29:36 +08:00
|
|
|
print $textfh $line;
|
2008-01-04 04:48:22 +08:00
|
|
|
}
|
2023-03-29 04:29:36 +08:00
|
|
|
if(!close($textfh)) {
|
2008-01-04 04:48:22 +08:00
|
|
|
$error = "Error: cannot close file $filename";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$error = "Error: cannot write file $filename";
|
|
|
|
}
|
|
|
|
return $error;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#***************************************************************************
|
|
|
|
# Display contents of the given file
|
|
|
|
#
|
|
|
|
sub display_file {
|
|
|
|
my $filename = $_[0];
|
|
|
|
print "=== Start of file $filename\n";
|
2023-03-29 04:29:36 +08:00
|
|
|
if(open(my $displayfh, "<", "$filename")) {
|
|
|
|
while(my $line = <$displayfh>) {
|
2008-01-04 04:48:22 +08:00
|
|
|
print "$line";
|
|
|
|
}
|
2023-03-29 04:29:36 +08:00
|
|
|
close $displayfh;
|
2008-01-04 04:48:22 +08:00
|
|
|
}
|
|
|
|
print "=== End of file $filename\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#***************************************************************************
|
|
|
|
# Display contents of the ssh daemon config file
|
|
|
|
#
|
|
|
|
sub display_sshdconfig {
|
|
|
|
display_file($sshdconfig);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#***************************************************************************
|
|
|
|
# Display contents of the ssh client config file
|
|
|
|
#
|
|
|
|
sub display_sshconfig {
|
|
|
|
display_file($sshconfig);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-08 21:54:02 +08:00
|
|
|
#***************************************************************************
|
|
|
|
# Display contents of the sftp client config file
|
|
|
|
#
|
|
|
|
sub display_sftpconfig {
|
|
|
|
display_file($sftpconfig);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-04 04:48:22 +08:00
|
|
|
#***************************************************************************
|
|
|
|
# Display contents of the ssh daemon log file
|
|
|
|
#
|
|
|
|
sub display_sshdlog {
|
2010-01-16 02:55:01 +08:00
|
|
|
die "error: \$sshdlog uninitialized" if(not defined $sshdlog);
|
2008-01-04 04:48:22 +08:00
|
|
|
display_file($sshdlog);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#***************************************************************************
|
|
|
|
# Display contents of the ssh client log file
|
|
|
|
#
|
|
|
|
sub display_sshlog {
|
2010-01-16 02:55:01 +08:00
|
|
|
die "error: \$sshlog uninitialized" if(not defined $sshlog);
|
2008-01-04 04:48:22 +08:00
|
|
|
display_file($sshlog);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-08 21:54:02 +08:00
|
|
|
#***************************************************************************
|
|
|
|
# Display contents of the sftp client log file
|
|
|
|
#
|
|
|
|
sub display_sftplog {
|
2010-01-16 02:55:01 +08:00
|
|
|
die "error: \$sftplog uninitialized" if(not defined $sftplog);
|
2008-02-08 21:54:02 +08:00
|
|
|
display_file($sftplog);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-04 04:48:22 +08:00
|
|
|
#***************************************************************************
|
|
|
|
# Find a file somewhere in the given path
|
|
|
|
#
|
|
|
|
sub find_file {
|
|
|
|
my $fn = $_[0];
|
|
|
|
shift;
|
|
|
|
my @path = @_;
|
|
|
|
foreach (@path) {
|
|
|
|
my $file = File::Spec->catfile($_, $fn);
|
2011-10-07 02:26:42 +08:00
|
|
|
if(-e $file && ! -d $file) {
|
2008-01-04 04:48:22 +08:00
|
|
|
return $file;
|
|
|
|
}
|
|
|
|
}
|
2023-03-28 10:58:00 +08:00
|
|
|
return "";
|
2008-01-04 04:48:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-07 02:26:42 +08:00
|
|
|
#***************************************************************************
|
|
|
|
# Find an executable file somewhere in the given path
|
|
|
|
#
|
|
|
|
sub find_exe_file {
|
|
|
|
my $fn = $_[0];
|
|
|
|
shift;
|
|
|
|
my @path = @_;
|
2019-05-19 05:32:04 +08:00
|
|
|
my $xext = exe_ext('SSH');
|
2011-10-07 02:26:42 +08:00
|
|
|
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$/));
|
|
|
|
}
|
|
|
|
}
|
2023-03-28 10:58:00 +08:00
|
|
|
return "";
|
2011-10-07 02:26:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-04 04:48:22 +08:00
|
|
|
#***************************************************************************
|
|
|
|
# Find a file in environment path or in our sftppath
|
|
|
|
#
|
2011-10-07 02:26:42 +08:00
|
|
|
sub find_file_spath {
|
2008-01-04 04:48:22 +08:00
|
|
|
my $filename = $_[0];
|
|
|
|
my @spath;
|
|
|
|
push(@spath, File::Spec->path());
|
|
|
|
push(@spath, @sftppath);
|
|
|
|
return find_file($filename, @spath);
|
|
|
|
}
|
|
|
|
|
2011-10-07 02:26:42 +08:00
|
|
|
|
2011-01-20 03:35:02 +08:00
|
|
|
#***************************************************************************
|
2011-10-07 02:26:42 +08:00
|
|
|
# Find an executable file in environment path or in our httptlssrvpath
|
2011-01-20 03:35:02 +08:00
|
|
|
#
|
2011-10-07 02:26:42 +08:00
|
|
|
sub find_exe_file_hpath {
|
|
|
|
my $filename = $_[0];
|
|
|
|
my @hpath;
|
|
|
|
push(@hpath, File::Spec->path());
|
|
|
|
push(@hpath, @httptlssrvpath);
|
|
|
|
return find_exe_file($filename, @hpath);
|
2011-01-20 03:35:02 +08:00
|
|
|
}
|
2008-01-04 04:48:22 +08:00
|
|
|
|
2011-10-07 02:26:42 +08:00
|
|
|
|
2008-01-04 04:48:22 +08:00
|
|
|
#***************************************************************************
|
|
|
|
# Find ssh daemon and return canonical filename
|
|
|
|
#
|
|
|
|
sub find_sshd {
|
2011-10-07 02:26:42 +08:00
|
|
|
return find_file_spath($sshdexe);
|
2008-01-04 04:48:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#***************************************************************************
|
|
|
|
# Find ssh client and return canonical filename
|
|
|
|
#
|
|
|
|
sub find_ssh {
|
2011-10-07 02:26:42 +08:00
|
|
|
return find_file_spath($sshexe);
|
2008-01-04 04:48:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#***************************************************************************
|
|
|
|
# Find sftp-server plugin and return canonical filename
|
|
|
|
#
|
2008-02-08 21:54:02 +08:00
|
|
|
sub find_sftpsrv {
|
2011-10-07 02:26:42 +08:00
|
|
|
return find_file_spath($sftpsrvexe);
|
2008-02-08 21:54:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#***************************************************************************
|
|
|
|
# Find sftp client and return canonical filename
|
|
|
|
#
|
2008-01-04 04:48:22 +08:00
|
|
|
sub find_sftp {
|
2011-10-07 02:26:42 +08:00
|
|
|
return find_file_spath($sftpexe);
|
2008-01-04 04:48:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#***************************************************************************
|
|
|
|
# Find ssh-keygen and return canonical filename
|
|
|
|
#
|
|
|
|
sub find_sshkeygen {
|
2011-10-07 02:26:42 +08:00
|
|
|
return find_file_spath($sshkeygenexe);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#***************************************************************************
|
|
|
|
# Find httptlssrv (gnutls-serv) and return canonical filename
|
|
|
|
#
|
|
|
|
sub find_httptlssrv {
|
2023-02-15 22:04:07 +08:00
|
|
|
my $p = find_exe_file_hpath($httptlssrvexe);
|
2023-03-06 16:47:16 +08:00
|
|
|
if($p) {
|
|
|
|
my @o = `"$p" -l`;
|
|
|
|
my $found;
|
|
|
|
for(@o) {
|
|
|
|
if(/Key exchange: SRP/) {
|
|
|
|
$found = 1;
|
|
|
|
last;
|
|
|
|
}
|
2023-02-15 22:04:07 +08:00
|
|
|
}
|
2023-03-06 16:47:16 +08:00
|
|
|
return $p if($found);
|
2023-02-15 22:04:07 +08:00
|
|
|
}
|
2023-03-28 10:58:00 +08:00
|
|
|
return "";
|
2008-01-04 04:48:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#***************************************************************************
|
|
|
|
# Return version info for the given ssh client or server binaries
|
|
|
|
#
|
|
|
|
sub sshversioninfo {
|
|
|
|
my $sshbin = $_[0]; # canonical filename
|
|
|
|
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 {
|
2014-12-21 09:42:29 +08:00
|
|
|
my $cmd = ($sshbin =~ /$sshdexe$/) ? "\"$sshbin\" -?" : "\"$sshbin\" -V";
|
2008-01-04 04:48:22 +08:00
|
|
|
$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;
|
|
|
|
}
|
2018-11-20 04:05:57 +08:00
|
|
|
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;
|
|
|
|
}
|
2008-01-04 04:48:22 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#***************************************************************************
|
|
|
|
# End of library
|
|
|
|
1;
|