sshhelp.pm 9.8 KB

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