| 1 |
#!/usr/bin/env perl |
| 2 |
# Simulate a tunneling proxy to a HTTPS URL that needs authentication. |
| 3 |
# Use two connections (Connection: close) |
| 4 |
|
| 5 |
use strict; |
| 6 |
use warnings; |
| 7 |
|
| 8 |
use WgetFeature qw(https); |
| 9 |
use WgetTests; # For $WGETPATH. |
| 10 |
|
| 11 |
my $cert_path; |
| 12 |
my $key_path; |
| 13 |
my $srcdir; |
| 14 |
|
| 15 |
our $VALGRIND_SUPP_FILE; |
| 16 |
|
| 17 |
if (@ARGV) { |
| 18 |
$srcdir = shift @ARGV; |
| 19 |
} elsif (defined $ENV{srcdir}) { |
| 20 |
$srcdir = $ENV{srcdir}; |
| 21 |
} |
| 22 |
|
| 23 |
if (defined $srcdir) { |
| 24 |
$key_path = "$srcdir/certs/server-key.pem"; |
| 25 |
$cert_path = "$srcdir/certs/server-cert.pem"; |
| 26 |
} else { |
| 27 |
$key_path = "certs/server-key.pem"; |
| 28 |
$cert_path = "certs/server-cert.pem"; |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
use HTTP::Daemon; |
| 33 |
use HTTP::Request; |
| 34 |
# Skip this test rather than fail it when the module isn't installed |
| 35 |
if (!eval {require IO::Socket::SSL;1;}) { |
| 36 |
print STDERR "This test needs the perl module \"IO::Socket::SSL\".\n"; |
| 37 |
print STDERR "Install e.g. on Debian with 'apt-get install libio-socket-ssl-perl'\n"; |
| 38 |
print STDERR " or if using cpanminus 'cpanm IO::Socket::SSL' could be used to install it.\n"; |
| 39 |
exit 77; # skip |
| 40 |
} |
| 41 |
IO::Socket::SSL->import(); |
| 42 |
|
| 43 |
my $SOCKET = HTTP::Daemon->new (LocalAddr => 'localhost', |
| 44 |
ReuseAddr => 1) or die "Cannot create server!!!"; |
| 45 |
|
| 46 |
sub get_request { |
| 47 |
my $conn = shift; |
| 48 |
my $content = ''; |
| 49 |
my $line; |
| 50 |
|
| 51 |
while (defined ($line = <$conn>)) { |
| 52 |
$content .= $line; |
| 53 |
last if $line eq "\r\n"; |
| 54 |
} |
| 55 |
|
| 56 |
my $rqst = HTTP::Request->parse($content) |
| 57 |
or die "Couldn't parse request:\n$content\n"; |
| 58 |
|
| 59 |
return $rqst; |
| 60 |
} |
| 61 |
|
| 62 |
sub do_server { |
| 63 |
my ($synch_callback) = @_; |
| 64 |
my $s = $SOCKET; |
| 65 |
my $conn; |
| 66 |
my $rqst; |
| 67 |
my $rspn; |
| 68 |
|
| 69 |
my %options = ( |
| 70 |
SSL_server => 1, |
| 71 |
SSL_passwd_cb => sub { return "Hello"; }); |
| 72 |
$options{SSL_cert_file} = $cert_path if ($cert_path); |
| 73 |
$options{SSL_key_file} = $key_path if ($key_path); |
| 74 |
my @options = %options; |
| 75 |
|
| 76 |
# sync with the parent |
| 77 |
$synch_callback->(); |
| 78 |
|
| 79 |
# Simulate a HTTPS proxy server with tunneling. |
| 80 |
|
| 81 |
for my $expect_inner_auth (0, 1) { |
| 82 |
$conn = $s->accept; |
| 83 |
$rqst = $conn->get_request; |
| 84 |
die "Method not CONNECT\n" if ($rqst->method ne 'CONNECT'); |
| 85 |
$rspn = HTTP::Response->new(200, 'OK'); |
| 86 |
$conn->send_response($rspn); |
| 87 |
|
| 88 |
# Now switch from plain to SSL (for simulating a transparent tunnel |
| 89 |
# to an HTTPS server). |
| 90 |
|
| 91 |
$conn = IO::Socket::SSL->new_from_fd($conn->fileno, @options) |
| 92 |
or die "Couldn't initiate SSL"; |
| 93 |
|
| 94 |
$rqst = &get_request($conn) |
| 95 |
or die "Didn't get proxied request\n"; |
| 96 |
|
| 97 |
unless ($expect_inner_auth) { |
| 98 |
die "Early proxied auth\n" if $rqst->header('Authorization'); |
| 99 |
|
| 100 |
$rspn = HTTP::Response->new(401, 'Unauthorized', [ |
| 101 |
'WWW-Authenticate' => 'Basic realm="gondor"', |
| 102 |
Connection => 'close' |
| 103 |
]); |
| 104 |
} else { |
| 105 |
die "No proxied auth\n" unless $rqst->header('Authorization'); |
| 106 |
|
| 107 |
$rspn = HTTP::Response->new(200, 'OK', [ |
| 108 |
'Content-Type' => 'text/plain', |
| 109 |
'Connection' => 'close', |
| 110 |
], "foobarbaz\n"); |
| 111 |
} |
| 112 |
|
| 113 |
$rspn->protocol('HTTP/1.0'); |
| 114 |
print STDERR "=====\n"; |
| 115 |
print STDERR $rspn->as_string; |
| 116 |
print STDERR "\n=====\n"; |
| 117 |
print $conn $rspn->as_string; |
| 118 |
|
| 119 |
$conn->close; |
| 120 |
} |
| 121 |
|
| 122 |
undef $conn; |
| 123 |
undef $s; |
| 124 |
} |
| 125 |
|
| 126 |
sub fork_server { |
| 127 |
pipe(FROM_CHILD, TO_PARENT) or die "Cannot create pipe!"; |
| 128 |
select((select(TO_PARENT), $| = 1)[0]); |
| 129 |
|
| 130 |
my $pid = fork(); |
| 131 |
if ($pid < 0) { |
| 132 |
die "Cannot fork"; |
| 133 |
} elsif ($pid == 0) { |
| 134 |
# child |
| 135 |
close FROM_CHILD; |
| 136 |
do_server(sub { print TO_PARENT "SYNC\n"; close TO_PARENT }); |
| 137 |
exit 0; |
| 138 |
} else { |
| 139 |
# parent |
| 140 |
close TO_PARENT; |
| 141 |
chomp(my $line = <FROM_CHILD>); |
| 142 |
close FROM_CHILD; |
| 143 |
} |
| 144 |
|
| 145 |
return $pid; |
| 146 |
} |
| 147 |
|
| 148 |
unlink "needs-auth.txt"; |
| 149 |
my $pid = &fork_server; |
| 150 |
|
| 151 |
my $cmdline = $WgetTest::WGETPATH . " --user=fiddle-dee-dee" |
| 152 |
. " --password=Dodgson -e https_proxy=localhost:{{port}}" |
| 153 |
. " --no-check-certificate" |
| 154 |
. " https://no.such.domain/needs-auth.txt"; |
| 155 |
$cmdline =~ s/\Q{{port}}/$SOCKET->sockport()/e; |
| 156 |
|
| 157 |
if (defined $srcdir) { |
| 158 |
$VALGRIND_SUPP_FILE = $srcdir . '/valgrind-suppressions-ssl'; |
| 159 |
} else { |
| 160 |
$VALGRIND_SUPP_FILE = './valgrind-suppressions-ssl'; |
| 161 |
} |
| 162 |
|
| 163 |
my $valgrind = $ENV{VALGRIND_TESTS}; |
| 164 |
if (!defined $valgrind || $valgrind eq "" || $valgrind == 0) { |
| 165 |
# Valgrind not requested - leave $cmdline as it is |
| 166 |
} elsif ($valgrind == 1) { |
| 167 |
$cmdline = |
| 168 |
'valgrind --suppressions=' . $VALGRIND_SUPP_FILE |
| 169 |
. ' --error-exitcode=301 --leak-check=yes --track-origins=yes --gen-suppressions=all ' |
| 170 |
. $cmdline; |
| 171 |
} else { |
| 172 |
$cmdline = $valgrind . " " . $cmdline; |
| 173 |
} |
| 174 |
|
| 175 |
my $code = system($cmdline . " 2>&1") >> 8; |
| 176 |
unlink "needs-auth.txt"; |
| 177 |
|
| 178 |
warn "Got code: $code\n" if $code; |
| 179 |
kill ('TERM', $pid); |
| 180 |
exit ($code != 0); |