1 |
root |
1.1 |
#!/opt/bin/perl |
2 |
|
|
|
3 |
|
|
=head1 NAME |
4 |
|
|
|
5 |
|
|
aemp - AnyEvent:MP utility |
6 |
|
|
|
7 |
|
|
=head1 SYNOPSIS |
8 |
|
|
|
9 |
|
|
aemp command args... |
10 |
|
|
|
11 |
|
|
# protocol commands |
12 |
|
|
aemp snd <port> <arg...> # send a message |
13 |
|
|
aemp mon <port> # wait till port is killed |
14 |
|
|
|
15 |
|
|
# node configuration: secret |
16 |
|
|
aemp gensecret # generate a random shared secret |
17 |
|
|
aemp setsecret <secret> # set the shared secret |
18 |
|
|
aemp clrsecret # remove the secret |
19 |
|
|
|
20 |
|
|
# node configuration: TLS |
21 |
|
|
aemp setcert <file> # set a certificate (key.pem + certificate.pem) |
22 |
|
|
aemp clrcert # remove certificate |
23 |
|
|
aemp gencert # generate a random certificate |
24 |
|
|
aemp forcetls <0|1> # enforce TLS connections? |
25 |
|
|
|
26 |
|
|
=head1 DESCRIPTION |
27 |
|
|
|
28 |
|
|
With aemp you can configure various aspects of AnyEvent::MP and it's protocol. |
29 |
|
|
|
30 |
|
|
=cut |
31 |
|
|
|
32 |
|
|
use common::sense; |
33 |
|
|
|
34 |
|
|
use Carp (); |
35 |
|
|
|
36 |
|
|
use AnyEvent; |
37 |
|
|
use AnyEvent::MP::Config; |
38 |
|
|
use AnyEvent::MP; |
39 |
|
|
|
40 |
|
|
sub run_cmd { |
41 |
|
|
my $cmd = shift; |
42 |
|
|
|
43 |
|
|
require POSIX; |
44 |
|
|
|
45 |
|
|
local $^F = 1023; #d# |
46 |
|
|
|
47 |
|
|
my $cv = AE::cv; |
48 |
|
|
|
49 |
|
|
my %redir; |
50 |
|
|
my @exe; |
51 |
|
|
|
52 |
|
|
while (@_) { |
53 |
|
|
my $type = shift; |
54 |
|
|
|
55 |
|
|
my $fd = $type =~ s/^(\d+)// ? $1 : undef; |
56 |
|
|
|
57 |
|
|
# fd> |
58 |
|
|
if ($type eq ">") { |
59 |
|
|
my ($pr, $pw) = AnyEvent::Util::portable_pipe; |
60 |
|
|
my $cb = shift; |
61 |
|
|
|
62 |
|
|
$cv->begin; |
63 |
|
|
my $w; $w = AE::io $pr, 0, |
64 |
|
|
"SCALAR" eq ref $cb |
65 |
|
|
? sub { |
66 |
|
|
sysread $pr, $$cb, 8192, length $$cb |
67 |
|
|
and return; |
68 |
|
|
undef $w; $cv->end; |
69 |
|
|
} |
70 |
|
|
: sub { |
71 |
|
|
my $buf; |
72 |
|
|
sysread $pr, $buf, 8192 |
73 |
|
|
and return $cb->($buf); |
74 |
|
|
undef $w; $cv->end; |
75 |
|
|
} |
76 |
|
|
; |
77 |
|
|
$redir{defined $fd ? $fd : 1} = $pw; |
78 |
|
|
|
79 |
|
|
} elsif ($type =~ s/^>//) { |
80 |
|
|
push @exe, sub { |
81 |
|
|
open my $fh, ">", $type |
82 |
|
|
or POSIX::_exit (125); |
83 |
|
|
$redir{defined $fd ? $fd : 1} = $fh; |
84 |
|
|
}; |
85 |
|
|
|
86 |
|
|
} elsif ($type =~ s/^<//) { |
87 |
|
|
push @exe, sub { |
88 |
|
|
open my $fh, "<", $type |
89 |
|
|
or POSIX::_exit (125); |
90 |
|
|
$redir{defined $fd ? $fd : 0} = $fh; |
91 |
|
|
}; |
92 |
|
|
} |
93 |
|
|
} |
94 |
|
|
|
95 |
|
|
my $pid = fork; |
96 |
|
|
|
97 |
|
|
defined $pid |
98 |
|
|
or Carp::croak "fork: $!"; |
99 |
|
|
|
100 |
|
|
unless ($pid) { |
101 |
|
|
# step 1, execute |
102 |
|
|
$_->() for @exe; |
103 |
|
|
|
104 |
|
|
# step 2, move any existing fd's out of the way |
105 |
|
|
my @oldfh; |
106 |
|
|
for my $fh (values %redir) { |
107 |
|
|
push @oldfh, $fh; |
108 |
|
|
$fh = fileno $fh; |
109 |
|
|
|
110 |
|
|
defined ($fh = POSIX::dup ($fh)) or POSIX::_exit (124) |
111 |
|
|
while exists $redir{$fh}; |
112 |
|
|
} |
113 |
|
|
|
114 |
|
|
# step 3, execute redirects |
115 |
|
|
while (my ($k, $v) = each %redir) { |
116 |
|
|
defined POSIX::dup2 ($v, $k) |
117 |
|
|
or POSIX::_exit (123); |
118 |
|
|
} |
119 |
|
|
|
120 |
|
|
# step 4, close everything else |
121 |
|
|
for (3..1023) { #TODO |
122 |
|
|
POSIX::close ($_) |
123 |
|
|
unless exists $redir{$_}; |
124 |
|
|
} |
125 |
|
|
|
126 |
|
|
exec @$cmd; |
127 |
|
|
|
128 |
|
|
POSIX::_exit (126); |
129 |
|
|
} |
130 |
|
|
|
131 |
|
|
close $_ for values %redir; |
132 |
|
|
|
133 |
|
|
my $status; |
134 |
|
|
$cv->begin (sub { shift->send ($status) }); |
135 |
|
|
my $cw; $cw = AE::child $pid, sub { |
136 |
|
|
$status = $_[1]; |
137 |
|
|
undef $cw; $cv->end; |
138 |
|
|
}; |
139 |
|
|
|
140 |
|
|
$cv |
141 |
|
|
} |
142 |
|
|
|
143 |
|
|
sub my_run_cmd { |
144 |
|
|
my ($cmd) = @_; |
145 |
|
|
|
146 |
|
|
my $cv = &run_cmd; |
147 |
|
|
my $status = $cv->recv; |
148 |
|
|
|
149 |
|
|
$status |
150 |
|
|
and die "@$cmd: command failed with exit status $status."; |
151 |
|
|
} |
152 |
|
|
|
153 |
|
|
sub gen_cert { |
154 |
|
|
my_run_cmd [qw(openssl req -new -nodes -x509 -days 3650 -batch -newkey rsa:2048 -keyout /dev/fd/3)], |
155 |
|
|
"</dev/null", |
156 |
|
|
">" , \my $cert, |
157 |
|
|
"3>", \my $key, |
158 |
|
|
"2>/dev/null"; |
159 |
|
|
|
160 |
|
|
"$cert$key" |
161 |
|
|
} |
162 |
|
|
|
163 |
|
|
our $cfg = \%AnyEvent::MP::Config::CFG; |
164 |
|
|
our $nodecfg = $cfg->{node}{"#default"} ||= {}; |
165 |
|
|
|
166 |
|
|
sub resolve_port { |
167 |
|
|
my ($node, $port) = split /#/, $_[0], 2; |
168 |
|
|
|
169 |
|
|
$node = (resolve_node $node)->recv; |
170 |
|
|
"$node#$port" |
171 |
|
|
} |
172 |
|
|
|
173 |
|
|
our %CMD = ( |
174 |
|
|
snd => sub { |
175 |
|
|
my $port = resolve_port shift @ARGV; |
176 |
|
|
initialise_node "slave/", node_of $port; |
177 |
|
|
|
178 |
|
|
snd $port, @ARGV; |
179 |
|
|
|
180 |
|
|
my $cv = AE::cv; |
181 |
|
|
mon $port, $cv; |
182 |
|
|
my $reply = port { &$cv }; |
183 |
|
|
snd node_of $port, relay => $reply, "ok"; |
184 |
|
|
|
185 |
|
|
print join " ", $cv->recv, "\n"; |
186 |
|
|
}, |
187 |
|
|
|
188 |
|
|
mon => sub { |
189 |
|
|
my $port = resolve_port shift @ARGV; |
190 |
|
|
initialise_node "slave/", node_of $port; |
191 |
|
|
|
192 |
|
|
mon $port, my $cv = AE::cv; |
193 |
|
|
print join " ", $cv->recv, "\n"; |
194 |
|
|
}, |
195 |
|
|
|
196 |
|
|
setsecret => sub { |
197 |
|
|
@ARGV == 1 |
198 |
|
|
or die "shared secret missing\n"; |
199 |
|
|
|
200 |
|
|
$nodecfg->{secret} = shift @ARGV; |
201 |
|
|
++$cfg->{dirty}; |
202 |
|
|
}, |
203 |
|
|
gensecret => sub { |
204 |
|
|
$nodecfg->{secret} = AnyEvent::MP::Base::asciibits AnyEvent::MP::Base::nonce 64; |
205 |
|
|
++$cfg->{dirty}; |
206 |
|
|
}, |
207 |
|
|
clrsecret => sub { |
208 |
|
|
delete $nodecfg->{secret}; |
209 |
|
|
++$cfg->{dirty}; |
210 |
|
|
}, |
211 |
|
|
|
212 |
|
|
setcert => sub { |
213 |
|
|
@ARGV == 1 |
214 |
|
|
or die "key+certificate pem filename missing\n"; |
215 |
|
|
|
216 |
|
|
open my $fh, "<", $ARGV[0] |
217 |
|
|
or die "$ARGV[0]: $!"; |
218 |
|
|
|
219 |
|
|
local $/; |
220 |
|
|
$nodecfg->{cert} = <$fh>; |
221 |
|
|
++$cfg->{dirty}; |
222 |
|
|
}, |
223 |
|
|
gencert => sub { |
224 |
|
|
$nodecfg->{cert} = gen_cert; |
225 |
|
|
++$cfg->{dirty}; |
226 |
|
|
}, |
227 |
|
|
clrcert => sub { |
228 |
|
|
delete $nodecfg->{cert}; |
229 |
|
|
++$cfg->{dirty}; |
230 |
|
|
}, |
231 |
|
|
forcetls => sub { |
232 |
|
|
@ARGV == 1 |
233 |
|
|
or die "enable value missing\n"; |
234 |
|
|
|
235 |
|
|
$nodecfg->{forcetls} = !!shift @ARGV; |
236 |
|
|
++$cfg->{dirty}; |
237 |
|
|
}, |
238 |
|
|
); |
239 |
|
|
|
240 |
|
|
sub docmd { |
241 |
|
|
my $cmd = shift @ARGV; |
242 |
|
|
|
243 |
|
|
$CMD{$cmd} |
244 |
|
|
or die "$cmd: no such aemp command (try man aemp)"; |
245 |
|
|
|
246 |
|
|
$CMD{$cmd}(); |
247 |
|
|
} |
248 |
|
|
|
249 |
|
|
@ARGV |
250 |
|
|
or die "Usage: aemp subcommand ... (try man aemp)\n"; |
251 |
|
|
|
252 |
|
|
docmd; |
253 |
|
|
|
254 |
|
|
|