Debian Codegraph @

deps/curl/tests/sshhelp.pm

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