-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservers.pm
3002 lines (2634 loc) · 96.8 KB
/
servers.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#***************************************************************************
# _ _ ____ _
# Project ___| | | | _ \| |
# / __| | | | |_) | |
# | (__| |_| | _ <| |___
# \___|\___/|_| \_\_____|
#
# Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at https://curl.se/docs/copyright.html.
#
# 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.
#
# SPDX-License-Identifier: curl
#
###########################################################################
# This module contains functions that are useful for managing the lifecycle of
# test servers required when running tests. It is not intended for use within
# those servers, but rather for starting and stopping them.
package servers;
use IO::Socket;
use strict;
use warnings;
BEGIN {
use base qw(Exporter);
our @EXPORT = (
# variables
qw(
$SOCKSIN
$err_unexpected
$debugprotocol
$stunnel
),
# functions
qw(
initserverconfig
)
);
our @EXPORT_OK = (
# functions
qw(
checkcmd
clearlocks
serverfortest
stopserver
stopservers
subvariables
),
# for debugging only
qw(
protoport
)
);
}
use serverhelp qw(
serverfactors
servername_id
servername_str
servername_canon
server_pidfilename
server_portfilename
server_logfilename
);
use sshhelp qw(
$hstpubmd5f
$hstpubsha256f
$sshexe
$sftpexe
$sftpconfig
$sshdlog
$sftplog
$sftpcmds
display_sshdconfig
display_sftpconfig
display_sshdlog
display_sftplog
find_sshd
find_ssh
find_sftp
find_httptlssrv
sshversioninfo
);
use pathhelp qw(
exe_ext
os_is_win
sys_native_abs_path
);
use processhelp;
use globalconfig;
use testutil qw(
logmsg
runclient
runclientoutput
);
my %serverpidfile; # all server pid file names, identified by server id
my %serverportfile;# all server port file names, identified by server id
my $sshdvernum; # for socks server, ssh daemon version number
my $sshdverstr; # for socks server, ssh daemon version string
my $sshderror; # for socks server, ssh daemon version error
my %doesntrun; # servers that don't work, identified by pidfile
my %PORT = (nolisten => 47); # port we use for a local non-listening service
my $server_response_maxtime=13;
my $httptlssrv = find_httptlssrv();
my %run; # running server
my %runcert; # cert file currently in use by an ssl running server
my $CLIENTIP="127.0.0.1"; # address which curl uses for incoming connections
my $CLIENT6IP="[::1]"; # address which curl uses for incoming connections
my $posix_pwd=$pwd; # current working directory
my $h2cver = "h2c"; # this version is decided by the nghttp2 lib being used
my $portrange = 999; # space from which to choose a random port
# don't increase without making sure generated port
# numbers will always be valid (<=65535)
my $HOSTIP="127.0.0.1"; # address on which the test server listens
my $HOST6IP="[::1]"; # address on which the test server listens
my $HTTPUNIXPATH; # HTTP server Unix domain socket path
my $SOCKSUNIXPATH; # socks server Unix domain socket path
my $SSHSRVMD5 = "[uninitialized]"; # MD5 of ssh server public key
my $SSHSRVSHA256 = "[uninitialized]"; # SHA256 of ssh server public key
my $USER; # name of the current user
my $sshdid; # for socks server, ssh daemon version id
my $ftpchecktime=1; # time it took to verify our test FTP server
# Variables shared with runtests.pl
our $SOCKSIN="socksd-request.log"; # what curl sent to the SOCKS proxy
our $err_unexpected; # error instead of warning on server unexpectedly alive
our $debugprotocol; # nonzero for verbose server logs
our $stunnel; # path to stunnel command
#######################################################################
# Check for a command in the PATH of the test server.
#
sub checkcmd {
my ($cmd, @extrapaths)=@_;
my @paths=(split(m/[:]/, $ENV{'PATH'}), "/usr/sbin", "/usr/local/sbin",
"/sbin", "/usr/bin", "/usr/local/bin", @extrapaths);
for(@paths) {
if( -x "$_/$cmd" && ! -d "$_/$cmd") {
# executable bit but not a directory!
return "$_/$cmd";
}
}
return "";
}
#######################################################################
# Create a server socket on a random (unused) port, then close it and
# return the port number
#
sub getfreeport {
my ($ipnum) = @_;
my $server = IO::Socket->new(LocalPort => 0,
Domain => $ipnum == 6 ? AF_INET6 : AF_INET,
Type => SOCK_STREAM,
Reuse => 1,
Listen => 10 )
or die "Couldn't create tcp server socket: $@\n";
return $server->sockport();
}
use File::Temp qw/ tempfile/;
#######################################################################
# Initialize configuration variables
sub initserverconfig {
my ($fh, $socks) = tempfile("/tmp/curl-socksd-XXXXXXXX");
close($fh);
unlink($socks);
my ($f2, $http) = tempfile("/tmp/curl-http-XXXXXXXX");
close($f2);
unlink($http);
$SOCKSUNIXPATH = $socks; # SOCKS Unix domain socket
$HTTPUNIXPATH = $http; # HTTP Unix domain socket
$stunnel = checkcmd("stunnel4") || checkcmd("tstunnel") || checkcmd("stunnel");
# get the name of the current user
$USER = $ENV{USER}; # Linux
if (!$USER) {
$USER = $ENV{USERNAME}; # Windows
if (!$USER) {
$USER = $ENV{LOGNAME}; # Some Unix (I think)
}
}
init_serverpidfile_hash();
}
#######################################################################
# Load serverpidfile and serverportfile hashes with file names for all
# possible servers.
#
sub init_serverpidfile_hash {
for my $proto (('ftp', 'gopher', 'http', 'imap', 'pop3', 'smtp', 'http/2', 'http/3')) {
for my $ssl (('', 's')) {
for my $ipvnum ((4, 6)) {
for my $idnum ((1, 2, 3)) {
my $serv = servername_id("$proto$ssl", $ipvnum, $idnum);
my $pidf = server_pidfilename("$LOGDIR/$PIDDIR", "$proto$ssl",
$ipvnum, $idnum);
$serverpidfile{$serv} = $pidf;
my $portf = server_portfilename("$LOGDIR/$PIDDIR", "$proto$ssl",
$ipvnum, $idnum);
$serverportfile{$serv} = $portf;
}
}
}
}
for my $proto (('tftp', 'sftp', 'socks', 'ssh', 'rtsp', 'httptls',
'dict', 'smb', 'smbs', 'telnet', 'mqtt')) {
for my $ipvnum ((4, 6)) {
for my $idnum ((1, 2)) {
my $serv = servername_id($proto, $ipvnum, $idnum);
my $pidf = server_pidfilename("$LOGDIR/$PIDDIR", $proto, $ipvnum,
$idnum);
$serverpidfile{$serv} = $pidf;
my $portf = server_portfilename("$LOGDIR/$PIDDIR", $proto, $ipvnum,
$idnum);
$serverportfile{$serv} = $portf;
}
}
}
for my $proto (('http', 'imap', 'pop3', 'smtp', 'http/2', 'http/3')) {
for my $ssl (('', 's')) {
my $serv = servername_id("$proto$ssl", "unix", 1);
my $pidf = server_pidfilename("$LOGDIR/$PIDDIR", "$proto$ssl",
"unix", 1);
$serverpidfile{$serv} = $pidf;
my $portf = server_portfilename("$LOGDIR/$PIDDIR", "$proto$ssl",
"unix", 1);
$serverportfile{$serv} = $portf;
}
}
}
#######################################################################
# Kill the processes that still have lock files in a directory
#
sub clearlocks {
my $dir = $_[0];
my $done = 0;
if(os_is_win()) {
$dir = sys_native_abs_path($dir);
$dir =~ s/\//\\\\/g;
my $handle = "handle.exe";
if($ENV{"PROCESSOR_ARCHITECTURE"} =~ /64$/) {
$handle = "handle64.exe";
}
my @handles = `$handle $dir -accepteula -nobanner`;
for my $tryhandle (@handles) {
if($tryhandle =~ /^(\S+)\s+pid:\s+(\d+)\s+type:\s+(\w+)\s+([0-9A-F]+):\s+(.+)\r\r/) {
logmsg "Found $3 lock of '$5' ($4) by $1 ($2)\n";
# Ignore stunnel since we cannot do anything about its locks
if("$3" eq "File" && "$1" ne "tstunnel.exe") {
logmsg "Killing IMAGENAME eq $1 and PID eq $2\n";
system("taskkill.exe -f -fi \"IMAGENAME eq $1\" -fi \"PID eq $2\" >nul 2>&1");
$done = 1;
}
}
}
}
return $done;
}
#######################################################################
# Check if a given child process has just died. Reaps it if so.
#
sub checkdied {
my $pid = $_[0];
if((not defined $pid) || $pid <= 0) {
return 0;
}
use POSIX ":sys_wait_h";
my $rc = pidwait($pid, &WNOHANG);
return ($rc == $pid)?1:0;
}
##############################################################################
# This function makes sure the right set of server is running for the
# specified test case. This is a useful design when we run single tests as not
# all servers need to run then!
#
# Returns: a string, blank if everything is fine or a reason why it failed, and
# an integer:
# 0 for success
# 1 for an error starting the server
# 2 for not the first time getting an error starting the server
# 3 for a failure to stop a server in order to restart it
# 4 for an unsupported server type
#
sub serverfortest {
my (@what)=@_;
for(my $i = scalar(@what) - 1; $i >= 0; $i--) {
my $srvrline = $what[$i];
chomp $srvrline if($srvrline);
if($srvrline =~ /^(\S+)((\s*)(.*))/) {
my $server = "${1}";
my $lnrest = "${2}";
my $tlsext;
if($server =~ /^(httptls)(\+)(ext|srp)(\d*)(-ipv6|)$/) {
$server = "${1}${4}${5}";
$tlsext = uc("TLS-${3}");
}
if(! grep /^\Q$server\E$/, @protocols) {
if(substr($server,0,5) ne "socks") {
if($tlsext) {
return ("curl lacks $tlsext support", 4);
}
else {
return ("curl lacks $server server support", 4);
}
}
}
$what[$i] = "$server$lnrest" if($tlsext);
}
}
return &startservers(@what);
}
#######################################################################
# Start a new thread/process and run the given command line in there.
# Return the pids (yes plural) of the new child process to the parent.
#
sub startnew {
my ($cmd, $pidfile, $timeout, $fakepidfile)=@_;
logmsg "startnew: $cmd\n" if ($verbose);
my $child = fork();
if(not defined $child) {
logmsg "startnew: fork() failure detected\n";
return (-1,-1);
}
if(0 == $child) {
# Here we are the child. Run the given command.
# Flush output.
$| = 1;
# Put an "exec" in front of the command so that the child process
# keeps this child's process ID.
exec("exec $cmd") || die "Can't exec() $cmd: $!";
# exec() should never return back here to this process. We protect
# ourselves by calling die() just in case something goes really bad.
die "error: exec() has returned";
}
# Ugly hack but ssh client and gnutls-serv don't support pid files
if ($fakepidfile) {
if(open(my $out, ">", "$pidfile")) {
print $out $child . "\n";
close($out) || die "Failure writing pidfile";
logmsg "startnew: $pidfile faked with pid=$child\n" if($verbose);
}
else {
logmsg "startnew: failed to write fake $pidfile with pid=$child\n";
}
# could/should do a while connect fails sleep a bit and loop
portable_sleep($timeout);
if (checkdied($child)) {
logmsg "startnew: child process has failed to start\n" if($verbose);
return (-1,-1);
}
}
my $pid2 = 0;
my $count = $timeout;
while($count--) {
$pid2 = pidfromfile($pidfile);
if(($pid2 > 0) && pidexists($pid2)) {
# if $pid2 is valid, then make sure this pid is alive, as
# otherwise it is just likely to be the _previous_ pidfile or
# similar!
last;
}
if (checkdied($child)) {
logmsg "startnew: child process has died, server might start up\n"
if($verbose);
# We can't just abort waiting for the server with a
# return (-1,-1);
# because the server might have forked and could still start
# up normally. Instead, just reduce the amount of time we remain
# waiting.
$count >>= 2;
}
sleep(1);
}
# Return two PIDs, the one for the child process we spawned and the one
# reported by the server itself (in case it forked again on its own).
# Both (potentially) need to be killed at the end of the test.
return ($child, $pid2);
}
#######################################################################
# Return the port to use for the given protocol.
#
sub protoport {
my ($proto) = @_;
return $PORT{$proto} || "[not running]";
}
#######################################################################
# Stop a test server along with pids which aren't in the %run hash yet.
# This also stops all servers which are relative to the given one.
#
sub stopserver {
my ($server, $pidlist) = @_;
#
# kill sockfilter processes for pingpong relative server
#
if($server =~ /^(ftp|imap|pop3|smtp)s?(\d*)(-ipv6|)$/) {
my $proto = $1;
my $idnum = ($2 && ($2 > 1)) ? $2 : 1;
my $ipvnum = ($3 && ($3 =~ /6$/)) ? 6 : 4;
killsockfilters("$LOGDIR/$PIDDIR", $proto, $ipvnum, $idnum, $verbose);
}
#
# All servers relative to the given one must be stopped also
#
my @killservers;
if($server =~ /^(ftp|http|imap|pop3|smtp)s((\d*)(-ipv6|-unix|))$/) {
# given a stunnel based ssl server, also kill non-ssl underlying one
push @killservers, "${1}${2}";
}
elsif($server =~ /^(ftp|http|imap|pop3|smtp)((\d*)(-ipv6|-unix|))$/) {
# given a non-ssl server, also kill stunnel based ssl piggybacking one
push @killservers, "${1}s${2}";
}
elsif($server =~ /^(socks)((\d*)(-ipv6|))$/) {
# given a socks server, also kill ssh underlying one
push @killservers, "ssh${2}";
}
elsif($server =~ /^(ssh)((\d*)(-ipv6|))$/) {
# given a ssh server, also kill socks piggybacking one
push @killservers, "socks${2}";
}
if($server eq "http" or $server eq "https") {
# since the http2+3 server is a proxy that needs to know about the
# dynamic http port it too needs to get restarted when the http server
# is killed
push @killservers, "http/2";
push @killservers, "http/3";
}
push @killservers, $server;
#
# kill given pids and server relative ones clearing them in %run hash
#
foreach my $server (@killservers) {
if($run{$server}) {
# we must prepend a space since $pidlist may already contain a pid
$pidlist .= " $run{$server}";
$run{$server} = 0;
}
$runcert{$server} = 0 if($runcert{$server});
}
killpid($verbose, $pidlist);
#
# cleanup server pid files
#
my $result = 0;
foreach my $server (@killservers) {
my $pidfile = $serverpidfile{$server};
my $pid = processexists($pidfile);
if($pid > 0) {
if($err_unexpected) {
logmsg "ERROR: ";
$result = -1;
}
else {
logmsg "Warning: ";
}
logmsg "$server server unexpectedly alive\n";
killpid($verbose, $pid);
}
unlink($pidfile) if(-f $pidfile);
}
return $result;
}
#######################################################################
# Return flags to let curl use an external HTTP proxy
#
sub getexternalproxyflags {
return " --proxy $proxy_address ";
}
#######################################################################
# Verify that the server that runs on $ip, $port is our server. This also
# implies that we can speak with it, as there might be occasions when the
# server runs fine but we cannot talk to it ("Failed to connect to ::1: Can't
# assign requested address")
#
sub verifyhttp {
my ($proto, $ipvnum, $idnum, $ip, $port_or_path) = @_;
my $server = servername_id($proto, $ipvnum, $idnum);
my $bonus="";
# $port_or_path contains a path for Unix sockets, sws ignores the port
my $port = ($ipvnum eq "unix") ? 80 : $port_or_path;
my $verifyout = "$LOGDIR/".
servername_canon($proto, $ipvnum, $idnum) .'_verify.out';
unlink($verifyout) if(-f $verifyout);
my $verifylog = "$LOGDIR/".
servername_canon($proto, $ipvnum, $idnum) .'_verify.log';
unlink($verifylog) if(-f $verifylog);
if($proto eq "gopher") {
# gopher is funny
$bonus="1/";
}
my $flags = "--max-time $server_response_maxtime ";
$flags .= "--output $verifyout ";
$flags .= "--silent ";
$flags .= "--verbose ";
$flags .= "--globoff ";
$flags .= "--unix-socket '$port_or_path' " if $ipvnum eq "unix";
$flags .= "--insecure " if($proto eq 'https');
if($proxy_address) {
$flags .= getexternalproxyflags();
}
$flags .= "\"$proto://$ip:$port/${bonus}verifiedserver\"";
my $cmd = "$VCURL $flags 2>$verifylog";
# verify if our/any server is running on this port
logmsg "RUN: $cmd\n" if($verbose);
my $res = runclient($cmd);
$res >>= 8; # rotate the result
if($res & 128) {
logmsg "RUN: curl command died with a coredump\n";
return -1;
}
if($res && $verbose) {
logmsg "RUN: curl command returned $res\n";
if(open(my $file, "<", "$verifylog")) {
while(my $string = <$file>) {
logmsg "RUN: $string" if($string !~ /^([ \t]*)$/);
}
close($file);
}
}
my $data;
if(open(my $file, "<", "$verifyout")) {
while(my $string = <$file>) {
$data = $string;
last; # only want first line
}
close($file);
}
my $pid = 0;
if($data && ($data =~ /WE ROOLZ: (\d+)/)) {
$pid = 0+$1;
}
elsif($res == 6) {
# curl: (6) Couldn't resolve host '::1'
logmsg "RUN: failed to resolve host ($proto://$ip:$port/verifiedserver)\n";
return -1;
}
elsif($data || ($res && ($res != 7))) {
logmsg "RUN: Unknown server on our $server port: $port ($res)\n";
return -1;
}
return $pid;
}
#######################################################################
# Verify that the server that runs on $ip, $port is our server. This also
# implies that we can speak with it, as there might be occasions when the
# server runs fine but we cannot talk to it ("Failed to connect to ::1: Can't
# assign requested address")
#
sub verifyftp {
my ($proto, $ipvnum, $idnum, $ip, $port) = @_;
my $server = servername_id($proto, $ipvnum, $idnum);
my $time=time();
my $extra="";
my $verifylog = "$LOGDIR/".
servername_canon($proto, $ipvnum, $idnum) .'_verify.log';
unlink($verifylog) if(-f $verifylog);
if($proto eq "ftps") {
$extra .= "--insecure --ftp-ssl-control ";
}
my $flags = "--max-time $server_response_maxtime ";
$flags .= "--silent ";
$flags .= "--verbose ";
$flags .= "--globoff ";
$flags .= $extra;
if($proxy_address) {
$flags .= getexternalproxyflags();
}
$flags .= "\"$proto://$ip:$port/verifiedserver\"";
my $cmd = "$VCURL $flags 2>$verifylog";
# check if this is our server running on this port:
logmsg "RUN: $cmd\n" if($verbose);
my @data = runclientoutput($cmd);
my $res = $? >> 8; # rotate the result
if($res & 128) {
logmsg "RUN: curl command died with a coredump\n";
return -1;
}
my $pid = 0;
foreach my $line (@data) {
if($line =~ /WE ROOLZ: (\d+)/) {
# this is our test server with a known pid!
$pid = 0+$1;
last;
}
}
if($pid <= 0 && @data && $data[0]) {
# this is not a known server
logmsg "RUN: Unknown server on our $server port: $port\n";
return 0;
}
# we can/should use the time it took to verify the FTP server as a measure
# on how fast/slow this host/FTP is.
my $took = int(0.5+time()-$time);
if($verbose) {
logmsg "RUN: Verifying our test $server server took $took seconds\n";
}
$ftpchecktime = $took>=1?$took:1; # make sure it never is below 1
return $pid;
}
#######################################################################
# Verify that the server that runs on $ip, $port is our server. This also
# implies that we can speak with it, as there might be occasions when the
# server runs fine but we cannot talk to it ("Failed to connect to ::1: Can't
# assign requested address")
#
sub verifyrtsp {
my ($proto, $ipvnum, $idnum, $ip, $port) = @_;
my $server = servername_id($proto, $ipvnum, $idnum);
my $verifyout = "$LOGDIR/".
servername_canon($proto, $ipvnum, $idnum) .'_verify.out';
unlink($verifyout) if(-f $verifyout);
my $verifylog = "$LOGDIR/".
servername_canon($proto, $ipvnum, $idnum) .'_verify.log';
unlink($verifylog) if(-f $verifylog);
my $flags = "--max-time $server_response_maxtime ";
$flags .= "--output $verifyout ";
$flags .= "--silent ";
$flags .= "--verbose ";
$flags .= "--globoff ";
if($proxy_address) {
$flags .= getexternalproxyflags();
}
# currently verification is done using http
$flags .= "\"http://$ip:$port/verifiedserver\"";
my $cmd = "$VCURL $flags 2>$verifylog";
# verify if our/any server is running on this port
logmsg "RUN: $cmd\n" if($verbose);
my $res = runclient($cmd);
$res >>= 8; # rotate the result
if($res & 128) {
logmsg "RUN: curl command died with a coredump\n";
return -1;
}
if($res && $verbose) {
logmsg "RUN: curl command returned $res\n";
if(open(my $file, "<", "$verifylog")) {
while(my $string = <$file>) {
logmsg "RUN: $string" if($string !~ /^[ \t]*$/);
}
close($file);
}
}
my $data;
if(open(my $file, "<", "$verifyout")) {
while(my $string = <$file>) {
$data = $string;
last; # only want first line
}
close($file);
}
my $pid = 0;
if($data && ($data =~ /RTSP_SERVER WE ROOLZ: (\d+)/)) {
$pid = 0+$1;
}
elsif($res == 6) {
# curl: (6) Couldn't resolve host '::1'
logmsg "RUN: failed to resolve host ($proto://$ip:$port/verifiedserver)\n";
return -1;
}
elsif($data || ($res != 7)) {
logmsg "RUN: Unknown server on our $server port: $port\n";
return -1;
}
return $pid;
}
#######################################################################
# Verify that the ssh server has written out its pidfile, recovering
# the pid from the file and returning it if a process with that pid is
# actually alive, or a negative value if the process is dead.
#
sub verifyssh {
my ($proto, $ipvnum, $idnum, $ip, $port) = @_;
my $pidfile = server_pidfilename("$LOGDIR/$PIDDIR", $proto, $ipvnum,
$idnum);
my $pid = processexists($pidfile);
if($pid < 0) {
logmsg "RUN: SSH server has died after starting up\n";
}
return $pid;
}
#######################################################################
# Verify that we can connect to the sftp server, properly authenticate
# with generated config and key files and run a simple remote pwd.
#
sub verifysftp {
my ($proto, $ipvnum, $idnum, $ip, $port) = @_;
my $server = servername_id($proto, $ipvnum, $idnum);
my $verified = 0;
# Find out sftp client canonical file name
my $sftp = find_sftp();
if(!$sftp) {
logmsg "RUN: SFTP server cannot find $sftpexe\n";
return -1;
}
# Find out ssh client canonical file name
my $ssh = find_ssh();
if(!$ssh) {
logmsg "RUN: SFTP server cannot find $sshexe\n";
return -1;
}
# Connect to sftp server, authenticate and run a remote pwd
# command using our generated configuration and key files
my $cmd = "\"$sftp\" -b $LOGDIR/$PIDDIR/$sftpcmds -F $LOGDIR/$PIDDIR/$sftpconfig -S \"$ssh\" $ip > $sftplog 2>&1";
my $res = runclient($cmd);
# Search for pwd command response in log file
if(open(my $sftplogfile, "<", "$sftplog")) {
while(<$sftplogfile>) {
if(/^Remote working directory: /) {
$verified = 1;
last;
}
}
close($sftplogfile);
}
return $verified;
}
#######################################################################
# Verify that the non-stunnel HTTP TLS extensions capable server that runs
# on $ip, $port is our server. This also implies that we can speak with it,
# as there might be occasions when the server runs fine but we cannot talk
# to it ("Failed to connect to ::1: Can't assign requested address")
#
sub verifyhttptls {
my ($proto, $ipvnum, $idnum, $ip, $port) = @_;
my $server = servername_id($proto, $ipvnum, $idnum);
my $pidfile = server_pidfilename("$LOGDIR/$PIDDIR", $proto, $ipvnum,
$idnum);
my $verifyout = "$LOGDIR/".
servername_canon($proto, $ipvnum, $idnum) .'_verify.out';
unlink($verifyout) if(-f $verifyout);
my $verifylog = "$LOGDIR/".
servername_canon($proto, $ipvnum, $idnum) .'_verify.log';
unlink($verifylog) if(-f $verifylog);
my $flags = "--max-time $server_response_maxtime ";
$flags .= "--output $verifyout ";
$flags .= "--verbose ";
$flags .= "--globoff ";
$flags .= "--insecure ";
$flags .= "--tlsauthtype SRP ";
$flags .= "--tlsuser jsmith ";
$flags .= "--tlspassword abc ";
if($proxy_address) {
$flags .= getexternalproxyflags();
}
$flags .= "\"https://$ip:$port/verifiedserver\"";
my $cmd = "$VCURL $flags 2>$verifylog";
# verify if our/any server is running on this port
logmsg "RUN: $cmd\n" if($verbose);
my $res = runclient($cmd);
$res >>= 8; # rotate the result
if($res & 128) {
logmsg "RUN: curl command died with a coredump\n";
return -1;
}
if($res && $verbose) {
logmsg "RUN: curl command returned $res\n";
if(open(my $file, "<", "$verifylog")) {
while(my $string = <$file>) {
logmsg "RUN: $string" if($string !~ /^([ \t]*)$/);
}
close($file);
}
}
my $data;
if(open(my $file, "<", "$verifyout")) {
while(my $string = <$file>) {
$data .= $string;
}
close($file);
}
my $pid = 0;
if($data && ($data =~ /(GNUTLS|GnuTLS)/) && ($pid = processexists($pidfile))) {
if($pid < 0) {
logmsg "RUN: $server server has died after starting up\n";
}
return $pid;
}
elsif($res == 6) {
# curl: (6) Couldn't resolve host '::1'
logmsg "RUN: failed to resolve host (https://$ip:$port/verifiedserver)\n";
return -1;
}
elsif($data || ($res && ($res != 7))) {
logmsg "RUN: Unknown server on our $server port: $port ($res)\n";
return -1;
}
return $pid;
}
#######################################################################
# STUB for verifying socks
#
sub verifysocks {
my ($proto, $ipvnum, $idnum, $ip, $port) = @_;
my $pidfile = server_pidfilename("$LOGDIR/$PIDDIR", $proto, $ipvnum,
$idnum);
my $pid = processexists($pidfile);
if($pid < 0) {
logmsg "RUN: SOCKS server has died after starting up\n";
}
return $pid;
}
#######################################################################
# Verify that the server that runs on $ip, $port is our server. This also
# implies that we can speak with it, as there might be occasions when the
# server runs fine but we cannot talk to it ("Failed to connect to ::1: Can't
# assign requested address")
#
sub verifysmb {
my ($proto, $ipvnum, $idnum, $ip, $port) = @_;
my $server = servername_id($proto, $ipvnum, $idnum);
my $time=time();
my $extra="";
my $verifylog = "$LOGDIR/".
servername_canon($proto, $ipvnum, $idnum) .'_verify.log';
unlink($verifylog) if(-f $verifylog);
my $flags = "--max-time $server_response_maxtime ";
$flags .= "--silent ";
$flags .= "--verbose ";
$flags .= "--globoff ";
$flags .= "-u 'curltest:curltest' ";
$flags .= $extra;
$flags .= "\"$proto://$ip:$port/SERVER/verifiedserver\"";
my $cmd = "$VCURL $flags 2>$verifylog";
# check if this is our server running on this port:
logmsg "RUN: $cmd\n" if($verbose);
my @data = runclientoutput($cmd);
my $res = $? >> 8; # rotate the result
if($res & 128) {
logmsg "RUN: curl command died with a coredump\n";
return -1;
}
my $pid = 0;
foreach my $line (@data) {
if($line =~ /WE ROOLZ: (\d+)/) {
# this is our test server with a known pid!
$pid = 0+$1;
last;
}
}
if($pid <= 0 && @data && $data[0]) {
# this is not a known server
logmsg "RUN: Unknown server on our $server port: $port\n";
return 0;
}
# we can/should use the time it took to verify the server as a measure
# on how fast/slow this host is.
my $took = int(0.5+time()-$time);
if($verbose) {
logmsg "RUN: Verifying our test $server server took $took seconds\n";
}
return $pid;
}
#######################################################################
# Verify that the server that runs on $ip, $port is our server. This also
# implies that we can speak with it, as there might be occasions when the
# server runs fine but we cannot talk to it ("Failed to connect to ::1: Can't
# assign requested address")
#
sub verifytelnet {
my ($proto, $ipvnum, $idnum, $ip, $port) = @_;
my $server = servername_id($proto, $ipvnum, $idnum);
my $time=time();
my $extra="";
my $verifylog = "$LOGDIR/".
servername_canon($proto, $ipvnum, $idnum) .'_verify.log';
unlink($verifylog) if(-f $verifylog);
my $flags = "--max-time $server_response_maxtime ";
$flags .= "--silent ";
$flags .= "--verbose ";
$flags .= "--globoff ";
$flags .= "--upload-file - ";
$flags .= $extra;
$flags .= "\"$proto://$ip:$port\"";
my $cmd = "echo 'verifiedserver' | $VCURL $flags 2>$verifylog";
# check if this is our server running on this port:
logmsg "RUN: $cmd\n" if($verbose);
my @data = runclientoutput($cmd);
my $res = $? >> 8; # rotate the result
if($res & 128) {
logmsg "RUN: curl command died with a coredump\n";
return -1;
}
my $pid = 0;
foreach my $line (@data) {
if($line =~ /WE ROOLZ: (\d+)/) {
# this is our test server with a known pid!
$pid = 0+$1;
last;