sshhelp.pm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. #***************************************************************************
  2. # _ _ ____ _
  3. # Project ___| | | | _ \| |
  4. # / __| | | | |_) | |
  5. # | (__| |_| | _ <| |___
  6. # \___|\___/|_| \_\_____|
  7. #
  8. # Copyright (C) 1998 - 2020, 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. #***************************************************************************
  22. package sshhelp;
  23. use strict;
  24. use warnings;
  25. use Exporter;
  26. use File::Spec;
  27. #***************************************************************************
  28. # Global symbols allowed without explicit package name
  29. #
  30. use vars qw(
  31. @ISA
  32. @EXPORT_OK
  33. $sshdexe
  34. $sshexe
  35. $sftpsrvexe
  36. $sftpexe
  37. $sshkeygenexe
  38. $httptlssrvexe
  39. $sshdconfig
  40. $sshconfig
  41. $sftpconfig
  42. $knownhosts
  43. $sshdlog
  44. $sshlog
  45. $sftplog
  46. $sftpcmds
  47. $hstprvkeyf
  48. $hstpubkeyf
  49. $hstpubmd5f
  50. $cliprvkeyf
  51. $clipubkeyf
  52. @sftppath
  53. @httptlssrvpath
  54. );
  55. #***************************************************************************
  56. # Inherit Exporter's capabilities
  57. #
  58. @ISA = qw(Exporter);
  59. #***************************************************************************
  60. # Global symbols this module will export upon request
  61. #
  62. @EXPORT_OK = qw(
  63. $sshdexe
  64. $sshexe
  65. $sftpsrvexe
  66. $sftpexe
  67. $sshkeygenexe
  68. $sshdconfig
  69. $sshconfig
  70. $sftpconfig
  71. $knownhosts
  72. $sshdlog
  73. $sshlog
  74. $sftplog
  75. $sftpcmds
  76. $hstprvkeyf
  77. $hstpubkeyf
  78. $hstpubmd5f
  79. $cliprvkeyf
  80. $clipubkeyf
  81. display_sshdconfig
  82. display_sshconfig
  83. display_sftpconfig
  84. display_sshdlog
  85. display_sshlog
  86. display_sftplog
  87. dump_array
  88. exe_ext
  89. find_sshd
  90. find_ssh
  91. find_sftpsrv
  92. find_sftp
  93. find_sshkeygen
  94. find_httptlssrv
  95. logmsg
  96. sshversioninfo
  97. );
  98. #***************************************************************************
  99. # Global variables initialization
  100. #
  101. $sshdexe = 'sshd' .exe_ext('SSH'); # base name and ext of ssh daemon
  102. $sshexe = 'ssh' .exe_ext('SSH'); # base name and ext of ssh client
  103. $sftpsrvexe = 'sftp-server' .exe_ext('SSH'); # base name and ext of sftp-server
  104. $sftpexe = 'sftp' .exe_ext('SSH'); # base name and ext of sftp client
  105. $sshkeygenexe = 'ssh-keygen' .exe_ext('SSH'); # base name and ext of ssh-keygen
  106. $httptlssrvexe = 'gnutls-serv' .exe_ext('SSH'); # base name and ext of gnutls-serv
  107. $sshdconfig = 'curl_sshd_config'; # ssh daemon config file
  108. $sshconfig = 'curl_ssh_config'; # ssh client config file
  109. $sftpconfig = 'curl_sftp_config'; # sftp client config file
  110. $sshdlog = undef; # ssh daemon log file
  111. $sshlog = undef; # ssh client log file
  112. $sftplog = undef; # sftp client log file
  113. $sftpcmds = 'curl_sftp_cmds'; # sftp client commands batch file
  114. $knownhosts = 'curl_client_knownhosts'; # ssh knownhosts file
  115. $hstprvkeyf = 'curl_host_rsa_key'; # host private key file
  116. $hstpubkeyf = 'curl_host_rsa_key.pub'; # host public key file
  117. $hstpubmd5f = 'curl_host_rsa_key.pub_md5'; # md5 hash of host public key
  118. $cliprvkeyf = 'curl_client_key'; # client private key file
  119. $clipubkeyf = 'curl_client_key.pub'; # client public key file
  120. #***************************************************************************
  121. # Absolute paths where to look for sftp-server plugin, when not in PATH
  122. #
  123. @sftppath = qw(
  124. /usr/lib/openssh
  125. /usr/libexec/openssh
  126. /usr/libexec
  127. /usr/local/libexec
  128. /opt/local/libexec
  129. /usr/lib/ssh
  130. /usr/libexec/ssh
  131. /usr/sbin
  132. /usr/lib
  133. /usr/lib/ssh/openssh
  134. /usr/lib64/ssh
  135. /usr/lib64/misc
  136. /usr/lib/misc
  137. /usr/local/sbin
  138. /usr/freeware/bin
  139. /usr/freeware/sbin
  140. /usr/freeware/libexec
  141. /opt/ssh/sbin
  142. /opt/ssh/libexec
  143. );
  144. #***************************************************************************
  145. # Absolute paths where to look for httptlssrv (gnutls-serv), when not in PATH
  146. #
  147. @httptlssrvpath = qw(
  148. /usr/sbin
  149. /usr/libexec
  150. /usr/lib
  151. /usr/lib/misc
  152. /usr/lib64/misc
  153. /usr/local/bin
  154. /usr/local/sbin
  155. /usr/local/libexec
  156. /opt/local/bin
  157. /opt/local/sbin
  158. /opt/local/libexec
  159. /usr/freeware/bin
  160. /usr/freeware/sbin
  161. /usr/freeware/libexec
  162. /opt/gnutls/bin
  163. /opt/gnutls/sbin
  164. /opt/gnutls/libexec
  165. );
  166. #***************************************************************************
  167. # Return file extension for executable files on this operating system
  168. #
  169. sub exe_ext {
  170. my ($component, @arr) = @_;
  171. if ($ENV{'CURL_TEST_EXE_EXT'}) {
  172. return $ENV{'CURL_TEST_EXE_EXT'};
  173. }
  174. if ($ENV{'CURL_TEST_EXE_EXT_'.$component}) {
  175. return $ENV{'CURL_TEST_EXE_EXT_'.$component};
  176. }
  177. if ($^O eq 'MSWin32' || $^O eq 'cygwin' || $^O eq 'msys' ||
  178. $^O eq 'dos' || $^O eq 'os2') {
  179. return '.exe';
  180. }
  181. }
  182. #***************************************************************************
  183. # Create or overwrite the given file with lines from an array of strings
  184. #
  185. sub dump_array {
  186. my ($filename, @arr) = @_;
  187. my $error;
  188. if(!$filename) {
  189. $error = 'Error: Missing argument 1 for dump_array()';
  190. }
  191. elsif(open(TEXTFH, ">$filename")) {
  192. foreach my $line (@arr) {
  193. $line .= "\n" unless($line =~ /\n$/);
  194. print TEXTFH $line;
  195. }
  196. if(!close(TEXTFH)) {
  197. $error = "Error: cannot close file $filename";
  198. }
  199. }
  200. else {
  201. $error = "Error: cannot write file $filename";
  202. }
  203. return $error;
  204. }
  205. #***************************************************************************
  206. # Display a message
  207. #
  208. sub logmsg {
  209. my ($line) = @_;
  210. chomp $line if($line);
  211. $line .= "\n";
  212. print "$line";
  213. }
  214. #***************************************************************************
  215. # Display contents of the given file
  216. #
  217. sub display_file {
  218. my $filename = $_[0];
  219. print "=== Start of file $filename\n";
  220. if(open(DISPLAYFH, "<$filename")) {
  221. while(my $line = <DISPLAYFH>) {
  222. print "$line";
  223. }
  224. close DISPLAYFH;
  225. }
  226. print "=== End of file $filename\n";
  227. }
  228. #***************************************************************************
  229. # Display contents of the ssh daemon config file
  230. #
  231. sub display_sshdconfig {
  232. display_file($sshdconfig);
  233. }
  234. #***************************************************************************
  235. # Display contents of the ssh client config file
  236. #
  237. sub display_sshconfig {
  238. display_file($sshconfig);
  239. }
  240. #***************************************************************************
  241. # Display contents of the sftp client config file
  242. #
  243. sub display_sftpconfig {
  244. display_file($sftpconfig);
  245. }
  246. #***************************************************************************
  247. # Display contents of the ssh daemon log file
  248. #
  249. sub display_sshdlog {
  250. die "error: \$sshdlog uninitialized" if(not defined $sshdlog);
  251. display_file($sshdlog);
  252. }
  253. #***************************************************************************
  254. # Display contents of the ssh client log file
  255. #
  256. sub display_sshlog {
  257. die "error: \$sshlog uninitialized" if(not defined $sshlog);
  258. display_file($sshlog);
  259. }
  260. #***************************************************************************
  261. # Display contents of the sftp client log file
  262. #
  263. sub display_sftplog {
  264. die "error: \$sftplog uninitialized" if(not defined $sftplog);
  265. display_file($sftplog);
  266. }
  267. #***************************************************************************
  268. # Find a file somewhere in the given path
  269. #
  270. sub find_file {
  271. my $fn = $_[0];
  272. shift;
  273. my @path = @_;
  274. foreach (@path) {
  275. my $file = File::Spec->catfile($_, $fn);
  276. if(-e $file && ! -d $file) {
  277. return $file;
  278. }
  279. }
  280. }
  281. #***************************************************************************
  282. # Find an executable file somewhere in the given path
  283. #
  284. sub find_exe_file {
  285. my $fn = $_[0];
  286. shift;
  287. my @path = @_;
  288. my $xext = exe_ext('SSH');
  289. foreach (@path) {
  290. my $file = File::Spec->catfile($_, $fn);
  291. if(-e $file && ! -d $file) {
  292. return $file if(-x $file);
  293. return $file if(($xext) && (lc($file) =~ /\Q$xext\E$/));
  294. }
  295. }
  296. }
  297. #***************************************************************************
  298. # Find a file in environment path or in our sftppath
  299. #
  300. sub find_file_spath {
  301. my $filename = $_[0];
  302. my @spath;
  303. push(@spath, File::Spec->path());
  304. push(@spath, @sftppath);
  305. return find_file($filename, @spath);
  306. }
  307. #***************************************************************************
  308. # Find an executable file in environment path or in our httptlssrvpath
  309. #
  310. sub find_exe_file_hpath {
  311. my $filename = $_[0];
  312. my @hpath;
  313. push(@hpath, File::Spec->path());
  314. push(@hpath, @httptlssrvpath);
  315. return find_exe_file($filename, @hpath);
  316. }
  317. #***************************************************************************
  318. # Find ssh daemon and return canonical filename
  319. #
  320. sub find_sshd {
  321. return find_file_spath($sshdexe);
  322. }
  323. #***************************************************************************
  324. # Find ssh client and return canonical filename
  325. #
  326. sub find_ssh {
  327. return find_file_spath($sshexe);
  328. }
  329. #***************************************************************************
  330. # Find sftp-server plugin and return canonical filename
  331. #
  332. sub find_sftpsrv {
  333. return find_file_spath($sftpsrvexe);
  334. }
  335. #***************************************************************************
  336. # Find sftp client and return canonical filename
  337. #
  338. sub find_sftp {
  339. return find_file_spath($sftpexe);
  340. }
  341. #***************************************************************************
  342. # Find ssh-keygen and return canonical filename
  343. #
  344. sub find_sshkeygen {
  345. return find_file_spath($sshkeygenexe);
  346. }
  347. #***************************************************************************
  348. # Find httptlssrv (gnutls-serv) and return canonical filename
  349. #
  350. sub find_httptlssrv {
  351. return find_exe_file_hpath($httptlssrvexe);
  352. }
  353. #***************************************************************************
  354. # Return version info for the given ssh client or server binaries
  355. #
  356. sub sshversioninfo {
  357. my $sshbin = $_[0]; # canonical filename
  358. my $major;
  359. my $minor;
  360. my $patch;
  361. my $sshid;
  362. my $versnum;
  363. my $versstr;
  364. my $error;
  365. if(!$sshbin) {
  366. $error = 'Error: Missing argument 1 for sshversioninfo()';
  367. }
  368. elsif(! -x $sshbin) {
  369. $error = "Error: cannot read or execute $sshbin";
  370. }
  371. else {
  372. my $cmd = ($sshbin =~ /$sshdexe$/) ? "\"$sshbin\" -?" : "\"$sshbin\" -V";
  373. $error = "$cmd\n";
  374. foreach my $tmpstr (qx($cmd 2>&1)) {
  375. if($tmpstr =~ /OpenSSH[_-](\d+)\.(\d+)(\.(\d+))*/i) {
  376. $major = $1;
  377. $minor = $2;
  378. $patch = $4?$4:0;
  379. $sshid = 'OpenSSH';
  380. $versnum = (100*$major) + (10*$minor) + $patch;
  381. $versstr = "$sshid $major.$minor.$patch";
  382. $error = undef;
  383. last;
  384. }
  385. if($tmpstr =~ /OpenSSH[_-]for[_-]Windows[_-](\d+)\.(\d+)(\.(\d+))*/i) {
  386. $major = $1;
  387. $minor = $2;
  388. $patch = $4?$4:0;
  389. $sshid = 'OpenSSH-Windows';
  390. $versnum = (100*$major) + (10*$minor) + $patch;
  391. $versstr = "$sshid $major.$minor.$patch";
  392. $error = undef;
  393. last;
  394. }
  395. if($tmpstr =~ /Sun[_-]SSH[_-](\d+)\.(\d+)(\.(\d+))*/i) {
  396. $major = $1;
  397. $minor = $2;
  398. $patch = $4?$4:0;
  399. $sshid = 'SunSSH';
  400. $versnum = (100*$major) + (10*$minor) + $patch;
  401. $versstr = "$sshid $major.$minor.$patch";
  402. $error = undef;
  403. last;
  404. }
  405. $error .= $tmpstr;
  406. }
  407. chomp $error if($error);
  408. }
  409. return ($sshid, $versnum, $versstr, $error);
  410. }
  411. #***************************************************************************
  412. # End of library
  413. 1;