File: simple

package info (click to toggle)
spiped 1.6.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,328 kB
  • sloc: ansic: 11,951; sh: 1,081; makefile: 629; perl: 121
file content (137 lines) | stat: -rwxr-xr-x 3,265 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/perl

use v5.012;
use strict;
use warnings;

use Fcntl qw(O_RDONLY);
use File::Temp;
use Getopt::Std;
use POSIX qw(:sys_wait_h);

sub usage(;$)
{
	my ($err) = @_;
	$err //= 1;
	my $s = <<EOUSAGE ;
Usage:	simple [-c spipe] [-d plainport] [-e encport] [-s spiped] [-t string]
	-c	specify the path to spipe (default: spipe)
	-d	specify the port for the unencrypted nc listener
		(default: 8086)
	-e	specify the port for the encrypted spiped listener
		(default: 6502)
	-s	specify the path to spiped (default: spiped)
	-t	specify the text string to pass through the pipe
		(default: "mellon")
EOUSAGE
	if ($err) {
		die $s;
	} else {
		print $s;
	}
}

sub killclose($ @)
{
	my ($pids, @pipes) = @_;

	my @p = grep defined, values %{$pids};
	say "Sending SIGTERM to @p";
	kill 15, @p;
	say 'Closing '.scalar(@pipes).' file handles';
	close $_ for @pipes;
}

MAIN:
{
	my %opts;
	getopts 'c:d:e:s:t:', \%opts or usage;
	my $spipecmd = $opts{c} // 'spipe';
	my $spipedcmd = $opts{s} // 'spiped';
	my $decport = $opts{d} // 8086;
	my $encport = $opts{e} // 6502;
	my $token = $opts{t} // 'mellon';

	usage unless @ARGV == 0;

	my $key;
	{
		sysopen my $rand, '/dev/urandom', O_RDONLY or
		    die "Could not open /dev/urandom: $!\n";
		my $n = sysread $rand, $key, 32;
		if (!defined $n) {
			die "Could not read from /dev/urandom: $!\n";
		} elsif ($n < 32) {
			die "Could not read 32 bytes from /dev/urandom, only got $n\n";
		}
		close $rand;
	}

	my $keyfile = File::Temp->new(TEMPLATE => 'spiped-test.XXXXXX',
	    SUFFIX => '.key', TMPDIR => 1, CLEANUP => 1);
	print $keyfile $key or
	    die "Could not write the 32-byte random key to $keyfile: $!\n";
	close $keyfile or
	    die "Could not close the key file $keyfile: $!\n";

	say "Starting nc...";
	my %pid;
	$pid{nc} = open my $nc, '|-', 'nc', '-q', '0', '-l', '-p', $decport or
	    die "Could not execute 'nc': $!\n";
	if (!defined $pid{nc}) {
		die "Could not start nc: $!\n";
	}
	say "nc started with pid $pid{nc}";
	sleep 1;

	$pid{spiped} = fork;
	if (!defined $pid{spiped}) {
		my $msg = $!;
		killclose \%pid, $nc;
		die "Could not fork for spiped: $msg\n";
	} elsif ($pid{spiped} == 0) {
		exec { $spipedcmd } 'spiped', '-d', '-F', '-k', "$keyfile",
		    '-s', "127.0.0.1:$encport", '-t', "127.0.0.1:$decport";
		die "Could not run spiped: $!\n";
	}
	say "spiped started with pid $pid{spiped}";
	sleep 1;

	$pid{spipe} = open my $spipe, '-|', $spipecmd, '-k', "$keyfile",
	    '-t', "127.0.0.1:$encport";
	if (!defined $pid{spipe}) {
		my $msg = $!;
		killclose \%pid, $nc;
		die "Could not start spipe: $msg\n";
	}
	say "spipe started with pid $pid{spipe}";

	if (!say $nc $token) {
		my $msg = $!;
		killclose \%pid, $nc, $spipe;
		die "Could not write to the nc child: $msg\n";
	}
	say "Wrote '$token'";
	close $nc;
	my $ncstatus = $?;
	delete $pid{nc};
	if ($ncstatus != 0) {
		killclose \%pid, $spipe;
		die "The nc child did something weird, wait status $ncstatus\n";
	}

	my $line = <$spipe>;
	if (!defined $line) {
		my $msg = $!;
		killclose \%pid, $spipe;
		die "Could not read from the spipe child: $msg\n";
	}
	chomp $line;
	say "Read back '$line'";

	killclose \%pid, $spipe;
	if ($line ne $token) {
		die "Wrote '$token' yet read '$line'\n";
	}
	say 'Everything seems fine';
}