xref: /openbsd/regress/usr.sbin/relayd/remote.pl (revision cab60319)
1bf5ac567Sbluhm#!/usr/bin/perl
2*cab60319Sbluhm#	$OpenBSD: remote.pl,v 1.5 2014/07/10 10:19:06 bluhm Exp $
3bf5ac567Sbluhm
4a7ce14b0Sbluhm# Copyright (c) 2010-2013 Alexander Bluhm <bluhm@openbsd.org>
5bf5ac567Sbluhm#
6bf5ac567Sbluhm# Permission to use, copy, modify, and distribute this software for any
7bf5ac567Sbluhm# purpose with or without fee is hereby granted, provided that the above
8bf5ac567Sbluhm# copyright notice and this permission notice appear in all copies.
9bf5ac567Sbluhm#
10bf5ac567Sbluhm# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11bf5ac567Sbluhm# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12bf5ac567Sbluhm# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13bf5ac567Sbluhm# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14bf5ac567Sbluhm# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15bf5ac567Sbluhm# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16bf5ac567Sbluhm# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17bf5ac567Sbluhm
18bf5ac567Sbluhmuse strict;
19bf5ac567Sbluhmuse warnings;
20bf5ac567Sbluhmuse File::Basename;
21bf5ac567Sbluhmuse File::Copy;
22bf5ac567Sbluhmuse Socket;
23bf5ac567Sbluhmuse Socket6;
24bf5ac567Sbluhm
25bf5ac567Sbluhmuse Client;
26bf5ac567Sbluhmuse Relayd;
27bf5ac567Sbluhmuse Server;
28bf5ac567Sbluhmuse Remote;
29bf5ac567Sbluhmrequire 'funcs.pl';
30bf5ac567Sbluhm
31bf5ac567Sbluhmsub usage {
32bf5ac567Sbluhm	die <<"EOF";
33bf5ac567Sbluhmusage:
34bf5ac567Sbluhm    remote.pl localport remoteaddr remoteport [test-args.pl]
35bf5ac567Sbluhm	Run test with local client and server.  Remote relayd
36bf5ac567Sbluhm	forwarding from remoteaddr remoteport to server localport
37bf5ac567Sbluhm	has to be started manually.
38bf5ac567Sbluhm    remote.pl copy|splice listenaddr connectaddr connectport [test-args.pl]
39bf5ac567Sbluhm	Only start remote relayd.
40bf5ac567Sbluhm    remote.pl copy|splice localaddr remoteaddr remotessh [test-args.pl]
41bf5ac567Sbluhm	Run test with local client and server.  Remote relayd is
42bf5ac567Sbluhm	started automatically with ssh on remotessh.
43bf5ac567SbluhmEOF
44bf5ac567Sbluhm}
45bf5ac567Sbluhm
46bf5ac567Sbluhmmy $test;
47bf5ac567Sbluhmour %args;
48bf5ac567Sbluhmif (@ARGV and -f $ARGV[-1]) {
49bf5ac567Sbluhm	$test = pop;
50bf5ac567Sbluhm	do $test
51bf5ac567Sbluhm	    or die "Do test file $test failed: ", $@ || $!;
52bf5ac567Sbluhm}
53bf5ac567Sbluhmmy $mode =
54bf5ac567Sbluhm	@ARGV == 3 && $ARGV[0] =~ /^\d+$/ && $ARGV[2] =~ /^\d+$/ ? "manual" :
55bf5ac567Sbluhm	@ARGV == 4 && $ARGV[1] !~ /^\d+$/ && $ARGV[3] =~ /^\d+$/ ? "relay"  :
56bf5ac567Sbluhm	@ARGV == 4 && $ARGV[1] !~ /^\d+$/ && $ARGV[3] !~ /^\d+$/ ? "auto"   :
57bf5ac567Sbluhm	usage();
58bf5ac567Sbluhm
59bf5ac567Sbluhmmy $r;
60bf5ac567Sbluhmif ($mode eq "relay") {
61bf5ac567Sbluhm	my($rport) = find_ports(num => 1);
62bf5ac567Sbluhm	$r = Relayd->new(
63bf5ac567Sbluhm	    forward             => $ARGV[0],
64bf5ac567Sbluhm	    %{$args{relayd}},
65bf5ac567Sbluhm	    listendomain        => AF_INET,
66bf5ac567Sbluhm	    listenaddr          => $ARGV[1],
67bf5ac567Sbluhm	    listenport          => $rport,
68bf5ac567Sbluhm	    connectdomain       => AF_INET,
69bf5ac567Sbluhm	    connectaddr         => $ARGV[2],
70bf5ac567Sbluhm	    connectport         => $ARGV[3],
71bf5ac567Sbluhm	    logfile             => dirname($0)."/remote.log",
72bf5ac567Sbluhm	    conffile            => dirname($0)."/relayd.conf",
73bf5ac567Sbluhm	    testfile            => $test,
74bf5ac567Sbluhm	);
75bf5ac567Sbluhm	open(my $log, '<', $r->{logfile})
76bf5ac567Sbluhm	    or die "Remote log file open failed: $!";
77bf5ac567Sbluhm	$SIG{__DIE__} = sub {
78bf5ac567Sbluhm		die @_ if $^S;
79bf5ac567Sbluhm		copy($log, \*STDERR);
80bf5ac567Sbluhm		warn @_;
81bf5ac567Sbluhm		exit 255;
82bf5ac567Sbluhm	};
83bf5ac567Sbluhm	copy($log, \*STDERR);
84bf5ac567Sbluhm	$r->run;
85bf5ac567Sbluhm	copy($log, \*STDERR);
86bf5ac567Sbluhm	$r->up;
87bf5ac567Sbluhm	copy($log, \*STDERR);
88bf5ac567Sbluhm	print STDERR "listen sock: $ARGV[1] $rport\n";
89bf5ac567Sbluhm	<STDIN>;
90bf5ac567Sbluhm	copy($log, \*STDERR);
91bf5ac567Sbluhm	print STDERR "stdin closed\n";
92bf5ac567Sbluhm	$r->kill_child;
93bf5ac567Sbluhm	$r->down;
94bf5ac567Sbluhm	copy($log, \*STDERR);
95bf5ac567Sbluhm
96bf5ac567Sbluhm	exit;
97bf5ac567Sbluhm}
98bf5ac567Sbluhm
99*cab60319Sbluhmmy $redo = $args{lengths} && @{$args{lengths}};
100*cab60319Sbluhm$redo = 0 if $args{client}{http_vers};  # run only one persistent connection
101bf5ac567Sbluhmmy $s = Server->new(
102bf5ac567Sbluhm    func                => \&read_char,
103bf5ac567Sbluhm    %{$args{server}},
104bf5ac567Sbluhm    listendomain        => AF_INET,
105bf5ac567Sbluhm    listenaddr          => ($mode eq "auto" ? $ARGV[1] : undef),
106bf5ac567Sbluhm    listenport          => ($mode eq "manual" ? $ARGV[0] : undef),
107*cab60319Sbluhm    redo                => $redo,
108a3c410c1Sbluhm) unless $args{server}{noserver};
109bf5ac567Sbluhmif ($mode eq "auto") {
110bf5ac567Sbluhm	$r = Remote->new(
111bf5ac567Sbluhm	    forward             => $ARGV[0],
112bf5ac567Sbluhm	    logfile             => "relayd.log",
113bf5ac567Sbluhm	    testfile            => $test,
11414344d13Sbluhm	    %{$args{relayd}},
115bf5ac567Sbluhm	    remotessh           => $ARGV[3],
116bf5ac567Sbluhm	    listenaddr          => $ARGV[2],
117bf5ac567Sbluhm	    connectaddr         => $ARGV[1],
118a3c410c1Sbluhm	    connectport         => $s ? $s->{listenport} : 1,
119bf5ac567Sbluhm	);
120bf5ac567Sbluhm	$r->run->up;
121bf5ac567Sbluhm}
122bf5ac567Sbluhmmy $c = Client->new(
123bf5ac567Sbluhm    func                => \&write_char,
124bf5ac567Sbluhm    %{$args{client}},
125bf5ac567Sbluhm    connectdomain       => AF_INET,
126bf5ac567Sbluhm    connectaddr         => ($mode eq "manual" ? $ARGV[1] : $r->{listenaddr}),
127bf5ac567Sbluhm    connectport         => ($mode eq "manual" ? $ARGV[2] : $r->{listenport}),
12814344d13Sbluhm) unless $args{client}{noclient};
129bf5ac567Sbluhm
130a3c410c1Sbluhm$s->run unless $args{server}{noserver};
13114344d13Sbluhm$c->run->up unless $args{client}{noclient};
132a3c410c1Sbluhm$s->up unless $args{server}{noserver};
133bf5ac567Sbluhm
13414344d13Sbluhm$c->down unless $args{client}{noclient};
135a3c410c1Sbluhm$s->down unless $args{server}{noserver};
136bf5ac567Sbluhm$r->close_child;
137bf5ac567Sbluhm$r->down;
138bf5ac567Sbluhm
139a7ce14b0Sbluhmcheck_logs($c, $r, $s, %args);
140