sshhelp.pm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. #***************************************************************************
  2. # _ _ ____ _
  3. # Project ___| | | | _ \| |
  4. # / __| | | | |_) | |
  5. # | (__| |_| | _ <| |___
  6. # \___|\___/|_| \_\_____|
  7. #
  8. # Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. #
  10. # This software is licensed as described in the file COPYING, which
  11. # you should have received as part of this distribution. The terms
  12. # are also available at https://curl.se/docs/copyright.html.
  13. #
  14. # You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. # copies of the Software, and permit persons to whom the Software is
  16. # furnished to do so, under the terms of the COPYING file.
  17. #
  18. # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. # KIND, either express or implied.
  20. #
  21. # SPDX-License-Identifier: curl
  22. #
  23. #***************************************************************************
  24. package sshhelp;
  25. use strict;
  26. use warnings;
  27. BEGIN {
  28. use base qw(Exporter);
  29. our @EXPORT_OK = qw(
  30. $sshdexe
  31. $sshexe
  32. $sftpsrvexe
  33. $sftpexe
  34. $sshkeygenexe
  35. $sshdconfig
  36. $sshconfig
  37. $sftpconfig
  38. $knownhosts
  39. $sshdlog
  40. $sshlog
  41. $sftplog
  42. $sftpcmds
  43. $hstprvkeyf
  44. $hstpubkeyf
  45. $hstpubmd5f
  46. $hstpubsha256f
  47. $cliprvkeyf
  48. $clipubkeyf
  49. display_sshdconfig
  50. display_sshconfig
  51. display_sftpconfig
  52. display_sshdlog
  53. display_sshlog
  54. display_sftplog
  55. dump_array
  56. find_sshd
  57. find_ssh
  58. find_sftpsrv
  59. find_sftp
  60. find_sshkeygen
  61. find_httptlssrv
  62. sshversioninfo
  63. );
  64. }
  65. use File::Spec;
  66. use pathhelp qw(
  67. exe_ext
  68. );
  69. #***************************************************************************
  70. # Global variables initialization
  71. #
  72. our $sshdexe = 'sshd' .exe_ext('SSH'); # base name and ext of ssh daemon
  73. our $sshexe = 'ssh' .exe_ext('SSH'); # base name and ext of ssh client
  74. our $sftpsrvexe = 'sftp-server' .exe_ext('SSH'); # base name and ext of sftp-server
  75. our $sftpexe = 'sftp' .exe_ext('SSH'); # base name and ext of sftp client
  76. our $sshkeygenexe = 'ssh-keygen' .exe_ext('SSH'); # base name and ext of ssh-keygen
  77. our $httptlssrvexe = 'gnutls-serv' .exe_ext('SSH'); # base name and ext of gnutls-serv
  78. our $sshdconfig = 'curl_sshd_config'; # ssh daemon config file
  79. our $sshconfig = 'curl_ssh_config'; # ssh client config file
  80. our $sftpconfig = 'curl_sftp_config'; # sftp client config file
  81. our $sshdlog = undef; # ssh daemon log file
  82. our $sshlog = undef; # ssh client log file
  83. our $sftplog = undef; # sftp client log file
  84. our $sftpcmds = 'curl_sftp_cmds'; # sftp client commands batch file
  85. our $knownhosts = 'curl_client_knownhosts'; # ssh knownhosts file
  86. our $hstprvkeyf = 'curl_host_rsa_key'; # host private key file
  87. our $hstpubkeyf = 'curl_host_rsa_key.pub'; # host public key file
  88. our $hstpubmd5f = 'curl_host_rsa_key.pub_md5'; # md5 hash of host public key
  89. our $hstpubsha256f = 'curl_host_rsa_key.pub_sha256'; # sha256 hash of host public key
  90. our $cliprvkeyf = 'curl_client_key'; # client private key file
  91. our $clipubkeyf = 'curl_client_key.pub'; # client public key file
  92. #***************************************************************************
  93. # Absolute paths where to look for sftp-server plugin, when not in PATH
  94. #
  95. our @sftppath = qw(
  96. /usr/lib/openssh
  97. /usr/libexec/openssh
  98. /usr/libexec
  99. /usr/local/libexec
  100. /opt/local/libexec
  101. /usr/lib/ssh
  102. /usr/libexec/ssh
  103. /usr/sbin
  104. /usr/lib
  105. /usr/lib/ssh/openssh
  106. /usr/lib64/ssh
  107. /usr/lib64/misc
  108. /usr/lib/misc
  109. /usr/local/sbin
  110. /usr/freeware/bin
  111. /usr/freeware/sbin
  112. /usr/freeware/libexec
  113. /opt/ssh/sbin
  114. /opt/ssh/libexec
  115. );
  116. #***************************************************************************
  117. # Absolute paths where to look for httptlssrv (gnutls-serv), when not in PATH
  118. #
  119. our @httptlssrvpath = qw(
  120. /usr/sbin
  121. /usr/libexec
  122. /usr/lib
  123. /usr/lib/misc
  124. /usr/lib64/misc
  125. /usr/local/bin
  126. /usr/local/sbin
  127. /usr/local/libexec
  128. /opt/local/bin
  129. /opt/local/sbin
  130. /opt/local/libexec
  131. /usr/freeware/bin
  132. /usr/freeware/sbin
  133. /usr/freeware/libexec
  134. /opt/gnutls/bin
  135. /opt/gnutls/sbin
  136. /opt/gnutls/libexec
  137. );
  138. #***************************************************************************
  139. # Create or overwrite the given file with lines from an array of strings
  140. #
  141. sub dump_array {
  142. my ($filename, @arr) = @_;
  143. my $error;
  144. if(!$filename) {
  145. $error = 'Error: Missing argument 1 for dump_array()';
  146. }
  147. elsif(open(my $textfh, ">", $filename)) {
  148. foreach my $line (@arr) {
  149. $line .= "\n" if($line !~ /\n$/);
  150. print $textfh $line;
  151. }
  152. if(!close($textfh)) {
  153. $error = "Error: cannot close file $filename";
  154. }
  155. }
  156. else {
  157. $error = "Error: cannot write file $filename";
  158. }
  159. return $error;
  160. }
  161. #***************************************************************************
  162. # Display contents of the given file
  163. #
  164. sub display_file {
  165. my $filename = $_[0];
  166. print "=== Start of file $filename\n";
  167. if(open(my $displayfh, "<", "$filename")) {
  168. while(my $line = <$displayfh>) {
  169. print "$line";
  170. }
  171. close $displayfh;
  172. }
  173. print "=== End of file $filename\n";
  174. }
  175. #***************************************************************************
  176. # Display contents of the ssh daemon config file
  177. #
  178. sub display_sshdconfig {
  179. display_file($sshdconfig);
  180. }
  181. #***************************************************************************
  182. # Display contents of the ssh client config file
  183. #
  184. sub display_sshconfig {
  185. display_file($sshconfig);
  186. }
  187. #***************************************************************************
  188. # Display contents of the sftp client config file
  189. #
  190. sub display_sftpconfig {
  191. display_file($sftpconfig);
  192. }
  193. #***************************************************************************
  194. # Display contents of the ssh daemon log file
  195. #
  196. sub display_sshdlog {
  197. die "error: \$sshdlog uninitialized" if(not defined $sshdlog);
  198. display_file($sshdlog);
  199. }
  200. #***************************************************************************
  201. # Display contents of the ssh client log file
  202. #
  203. sub display_sshlog {
  204. die "error: \$sshlog uninitialized" if(not defined $sshlog);
  205. display_file($sshlog);
  206. }
  207. #***************************************************************************
  208. # Display contents of the sftp client log file
  209. #
  210. sub display_sftplog {
  211. die "error: \$sftplog uninitialized" if(not defined $sftplog);
  212. display_file($sftplog);
  213. }
  214. #***************************************************************************
  215. # Find a file somewhere in the given path
  216. #
  217. sub find_file {
  218. my $fn = $_[0];
  219. shift;
  220. my @path = @_;
  221. foreach (@path) {
  222. my $file = File::Spec->catfile($_, $fn);
  223. if(-e $file && ! -d $file) {
  224. return $file;
  225. }
  226. }
  227. return "";
  228. }
  229. #***************************************************************************
  230. # Find an executable file somewhere in the given path
  231. #
  232. sub find_exe_file {
  233. my $fn = $_[0];
  234. shift;
  235. my @path = @_;
  236. my $xext = exe_ext('SSH');
  237. foreach (@path) {
  238. my $file = File::Spec->catfile($_, $fn);
  239. if(-e $file && ! -d $file) {
  240. return $file if(-x $file);
  241. return $file if(($xext) && (lc($file) =~ /\Q$xext\E$/));
  242. }
  243. }
  244. return "";
  245. }
  246. #***************************************************************************
  247. # Find a file in environment path or in our sftppath
  248. #
  249. sub find_file_spath {
  250. my $filename = $_[0];
  251. my @spath;
  252. push(@spath, File::Spec->path());
  253. push(@spath, @sftppath);
  254. return find_file($filename, @spath);
  255. }
  256. #***************************************************************************
  257. # Find an executable file in environment path or in our httptlssrvpath
  258. #
  259. sub find_exe_file_hpath {
  260. my $filename = $_[0];
  261. my @hpath;
  262. push(@hpath, File::Spec->path());
  263. push(@hpath, @httptlssrvpath);
  264. return find_exe_file($filename, @hpath);
  265. }
  266. #***************************************************************************
  267. # Find ssh daemon and return canonical filename
  268. #
  269. sub find_sshd {
  270. return find_file_spath($sshdexe);
  271. }
  272. #***************************************************************************
  273. # Find ssh client and return canonical filename
  274. #
  275. sub find_ssh {
  276. return find_file_spath($sshexe);
  277. }
  278. #***************************************************************************
  279. # Find sftp-server plugin and return canonical filename
  280. #
  281. sub find_sftpsrv {
  282. return find_file_spath($sftpsrvexe);
  283. }
  284. #***************************************************************************
  285. # Find sftp client and return canonical filename
  286. #
  287. sub find_sftp {
  288. return find_file_spath($sftpexe);
  289. }
  290. #***************************************************************************
  291. # Find ssh-keygen and return canonical filename
  292. #
  293. sub find_sshkeygen {
  294. return find_file_spath($sshkeygenexe);
  295. }
  296. #***************************************************************************
  297. # Find httptlssrv (gnutls-serv) and return canonical filename
  298. #
  299. sub find_httptlssrv {
  300. my $p = find_exe_file_hpath($httptlssrvexe);
  301. if($p) {
  302. my @o = `"$p" -l`;
  303. my $found;
  304. for(@o) {
  305. if(/Key exchange: SRP/) {
  306. $found = 1;
  307. last;
  308. }
  309. }
  310. return $p if($found);
  311. }
  312. return "";
  313. }
  314. #***************************************************************************
  315. # Return version info for the given ssh client or server binaries
  316. #
  317. sub sshversioninfo {
  318. my $sshbin = $_[0]; # canonical filename
  319. my $major;
  320. my $minor;
  321. my $patch;
  322. my $sshid;
  323. my $versnum;
  324. my $versstr;
  325. my $error;
  326. if(!$sshbin) {
  327. $error = 'Error: Missing argument 1 for sshversioninfo()';
  328. }
  329. elsif(! -x $sshbin) {
  330. $error = "Error: cannot read or execute $sshbin";
  331. }
  332. else {
  333. my $cmd = ($sshbin =~ /$sshdexe$/) ? "\"$sshbin\" -?" : "\"$sshbin\" -V";
  334. $error = "$cmd\n";
  335. foreach my $tmpstr (qx($cmd 2>&1)) {
  336. if($tmpstr =~ /OpenSSH[_-](\d+)\.(\d+)(\.(\d+))*/i) {
  337. $major = $1;
  338. $minor = $2;
  339. $patch = $4?$4:0;
  340. $sshid = 'OpenSSH';
  341. $versnum = (100*$major) + (10*$minor) + $patch;
  342. $versstr = "$sshid $major.$minor.$patch";
  343. $error = undef;
  344. last;
  345. }
  346. if($tmpstr =~ /OpenSSH[_-]for[_-]Windows[_-](\d+)\.(\d+)(\.(\d+))*/i) {
  347. $major = $1;
  348. $minor = $2;
  349. $patch = $4?$4:0;
  350. $sshid = 'OpenSSH-Windows';
  351. $versnum = (100*$major) + (10*$minor) + $patch;
  352. $versstr = "$sshid $major.$minor.$patch";
  353. $error = undef;
  354. last;
  355. }
  356. if($tmpstr =~ /Sun[_-]SSH[_-](\d+)\.(\d+)(\.(\d+))*/i) {
  357. $major = $1;
  358. $minor = $2;
  359. $patch = $4?$4:0;
  360. $sshid = 'SunSSH';
  361. $versnum = (100*$major) + (10*$minor) + $patch;
  362. $versstr = "$sshid $major.$minor.$patch";
  363. $error = undef;
  364. last;
  365. }
  366. $error .= $tmpstr;
  367. }
  368. chomp $error if($error);
  369. }
  370. return ($sshid, $versnum, $versstr, $error);
  371. }
  372. #***************************************************************************
  373. # End of library
  374. 1;