SixXS/TIC/Server.pm

filter: Statementstmt 26.0% 36 / 138 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 36.0% 9 / 25 Podpod 100% 4 / 4 totaltotal 22.0% 49 / 222 CC 30 SCAR 32.3
CC
30
Cov
19%
_cmd_authenticate
72.0
run_command
42.0
_cmd_challenge
30.0
CRAP 25.2
« SixXS/TIC/Client.pm ↑ Summary SixXS.pm »
1 #!/usr/bin/perl
2  
3 package Net::SixXS::TIC::Server;
4  
5 1 ▶use v5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use version; our $VERSION = version->declare("v0.1.1");
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use Digest::MD5 'md5_hex';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use Net::SixXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Net::SixXS::Data::Tunnel;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 has username => (
18 is => 'rw',
19 isa => 'Str',
20 required => 1,
21 );
22  
23 1 has password => (
24 is => 'rw',
25 isa => 'Str',
26 required => 1,
27 );
28  
29 1 has tunnels => (
30 is => 'rw',
31 isa => 'HashRef[Net::SixXS::Data::Tunnel]',
32 required => 1,
33 );
34  
35 has clients => (
36 is => 'rw',
37 isa => 'HashRef',
38 required => 0,
39 0 ▶ default => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
40 1 );
41  
42 has server_name => (
43 is => 'rw',
44 isa => 'Str',
45 required => 0,
46 0 ▶ default => sub { 'Net-SixXS' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
47 1 );
48  
49 has server_version => (
50 is => 'rw',
51 isa => 'Str',
52 required => 0,
53 0 ▶ default => sub { "$Net::SixXS::VERSION" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
54 1 );
55  
56 has diag => (
57 is => 'rw',
58 does => 'Net::SixXS::Diag',
59 required => 0,
60 0 ▶ default => sub { Net::SixXS::diag },
Subroutine __ANON__: not called CC=1 SCAR=6.9
61 1 );
62  
63 sub greet_client($ $)
64 0 ▶{
Subroutine greet_client: not called CC=1 SCAR=6.9
Pod documented
65 0 my ($self, $client) = @_;
66  
67 0 $self->client_write_line($client,
68 '200 TIC server '.
69 $self->server_name.'/'.$self->server_version.' ready');
70 }
71  
72 sub client_write_line($ $ $)
73 0 ▶{
Subroutine client_write_line: not called CC=1 SCAR=6.9
Pod documented
74 0 my ($self, $client, $line) = @_;
75  
76 0 die ref($self)."->client_write_line() must be overridden!\n";
77 }
78  
79 sub _cmd_client($ $ $ $)
80 0 ▶{
Subroutine _cmd_client: not called CC=3 SCAR=24.8
81 0 my ($self, $client, $command, $args) = @_;
82  
83 0 ▶ if (defined $client->{client}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $client->{'client'}) { } 0 0 0
elsif (not @{$args;}) { } 0 0 0
84 0 return $self->client_write_line($client,
85 '500 Client identity already supplied');
86 0 } elsif (!@{$args}) {
87 0 return $self->client_write_line($client,
88 '500 No client identity supplied');
89 }
90  
91 0 $client->{client} = join ' ', @{$args};
92 0 return $self->client_write_line($client,
93 '200 Client identity accepted');
94 }
95  
96 sub _cmd_quit($ $ $ $)
97 0 ▶{
Subroutine _cmd_quit: not called CC=1 SCAR=6.9
98 0 my ($self, $client, $command, $args) = @_;
99  
100 0 $client->{shutdown} = 1;
101 0 return $self->client_write_line($client,
102 '200 Thanks for stopping by');
103 }
104  
105 sub _cmd_username($ $ $ $)
106 0 ▶{
Subroutine _cmd_username: not called CC=4 SCAR=30.0
107 0 my ($self, $client, $command, $args) = @_;
108  
109 0 ▶ if (@{$args} != 1) {
Branch: 3 branches
BranchTrueFalseTotal
if (@{$args;} != 1) { } 0 0 0
elsif (not defined $client->{'client'}) { } 0 0 0
elsif (defined $client->{'username'}) { } 0 0 0
110 0 return $self->client_write_line($client,
111 '500 Exactly one username must be supplied');
112 } elsif (!defined $client->{client}) {
113 0 return $self->client_write_line($client,
114 '500 Client identity not supplied yet');
115 } elsif (defined $client->{username}) {
116 0 return $self->client_write_line($client,
117 '500 Username already supplied');
118 }
119  
120 0 $client->{username} = shift @{$args};
121 0 return $self->client_write_line($client,
122 '200 Choose your authentication type');
123 }
124  
125 sub _cmd_challenge($ $ $ $)
126 0 ▶{
Subroutine _cmd_challenge: not called CC=5 SCAR=34.0
127 0 my ($self, $client, $command, $args) = @_;
128  
129 0 ▶ if (@{$args} != 1) {
Branch: 4 branches
BranchTrueFalseTotal
if (@{$args;} != 1) { } 0 0 0
elsif (not defined $client->{'client'}) { } 0 0 0
elsif (defined $client->{'auth_type'}) { } 0 0 0
elsif ($args->[0] ne 'md5') { } 0 0 0
130 0 return $self->client_write_line($client,
131 '500 Exactly one challenge type must be supplied');
132 } elsif (!defined $client->{client}) {
133 0 return $self->client_write_line($client,
134 '500 Username not supplied yet');
135 } elsif (defined $client->{auth_type}) {
136 0 return $self->client_write_line($client,
137 '500 Challenge type already supplied');
138 } elsif ($args->[0] ne 'md5') {
139 0 return $self->client_write_line($client,
140 '500 Only md5 authentication accepted');
141 }
142  
143 # FIXME: replace this with a cryptographically secure one
144 0 my $proto = join ' ', map int 65536 * rand, 1..16;
145 0 my $challenge = md5_hex($proto);
146 0 $client->{auth_type} = shift @{$args};
147 0 $client->{auth_challenge} = $challenge;
148 0 return $self->client_write_line($client,
149 "200 $challenge");
150 }
151  
152 sub _cmd_authenticate($ $ $ $)
153 0 ▶{
Subroutine _cmd_authenticate: not called CC=8 SCAR=42.8
154 0 my ($self, $client, $command, $args) = @_;
155  
156 0 ▶ if (@{$args} != 2) {
Branch: 4 branches
BranchTrueFalseTotal
if (@{$args;} != 2) { } 0 0 0
elsif (not defined $client->{'auth_type'}) { } 0 0 0
elsif (defined $client->{'auth'}) { } 0 0 0
elsif ($args->[0] ne $client->{'auth_type'}) { } 0 0 0
157 0 return $self->client_write_line($client,
158 '500 Exactly two arguments must be supplied');
159 } elsif (!defined $client->{auth_type}) {
160 0 return $self->client_write_line($client,
161 '500 Challenge type not supplied yet');
162 } elsif (defined $client->{auth}) {
163 0 return $self->client_write_line($client,
164 '500 Already authenticated');
165 } elsif ($args->[0] ne $client->{auth_type}) {
166 0 return $self->client_write_line($client,
167 '500 Challenge type mismatch');
168 }
169  
170 0 my $md5pass = md5_hex($self->password);
171 0 my $interm = "$client->{auth_challenge}$md5pass";
172 0 my $md5resp = md5_hex($interm);
173 0 ▶ if ($args->[1] ne $md5resp ||
Branch: 1 branch
BranchTrueFalseTotal
if ($args->[1] ne $md5resp or $self->username ne $client->{"username"}) 0 0 0
Condition0%0/3
$args->[1] ne $md5resp or $self->username ne $client->{"username"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->[1] ne $md5resp or $self->username ne $client->{"username"}
$args->[1] ne $md5resp $self->username ne $client->{"username"}
Truth table0%0/3
A or B
A= $args->[1] ne $md5respB= $self->username ne $client->{"username"}
ABresult
1X1
011
000
174 $self->username ne $client->{username}) {
175 0 delete $client->{$_} for
176 qw/auth_type auth_challenge username/;
177 0 return $self->client_write_line($client,
178 '500 Authentication failed');
179 }
180 0 $client->{auth} = 1;
181 0 return $self->client_write_line($client,
182 '200 Welcome and stuff');
183 }
184  
185 sub _cmd_tunnel_list($ $ $ $)
186 0 ▶{
Subroutine _cmd_tunnel_list: not called CC=4 SCAR=30.0
187 0 my ($self, $client, $command, $args) = @_;
188  
189 0 ▶ if (@{$args}) {
Branch: 2 branches
BranchTrueFalseTotal
if (@{$args;}) { } 0 0 0
elsif (not defined $client->{'auth'}) { } 0 0 0
190 0 return $self->client_write_line($client,
191 '500 No arguments to tunnel list');
192 } elsif (!defined $client->{auth}) {
193 0 return $self->client_write_line($client,
194 '500 Who are you?');
195 }
196  
197 0 $self->client_write_line($client, '201 Listing tunnels');
198 0 for my $t (values %{$self->tunnels}) {
199 0 $self->client_write_line($client,
200 $t->id.' '.$t->ipv6_local);
201 }
202 0 $self->client_write_line($client, '202 <id> <endpoint>');
203 }
204  
205 sub _cmd_tunnel_show($ $ $ $)
206 0 ▶{
Subroutine _cmd_tunnel_show: not called CC=5 SCAR=34.0
207 0 my ($self, $client, $command, $args) = @_;
208  
209 0 ▶ if (@{$args} != 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@{$args;} != 1) { } 0 0 0
elsif (not defined $client->{'auth'}) { } 0 0 0
210 0 return $self->client_write_line($client,
211 '500 Exactly one tunnel ID must be supplied');
212 } elsif (!defined $client->{auth}) {
213 0 return $self->client_write_line($client,
214 '500 Who are you?');
215 }
216  
217 0 my $tid = shift @{$args};
218 0 my $t = $self->tunnels->{$tid};
219 0 ▶ if (!defined $t) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $t) { } 0 0 0
220 0 $self->client_write_line($client, "500 Invalid tunnel $tid");
221 } else {
222 0 $self->client_write_line($client, "201 $tid");
223 0 my $h = $t->to_json;
224 0 $self->client_write_line($client, "$_: $h->{$_}") for
225 0 sort keys %{$h};
226 0 $self->client_write_line($client, "202 That's it");
227 }
228 }
229  
230 sub _cmd_get_unixtime($ $ $ $)
231 0 ▶{
Subroutine _cmd_get_unixtime: not called CC=1 SCAR=6.9
232 0 my ($self, $client, $command, $args) = @_;
233  
234 0 $self->client_write_line($client, "200 ".time);
235 }
236  
237 1 my %cmds = (
238 authenticate => \&_cmd_authenticate,
239 challenge => \&_cmd_challenge,
240 client => \&_cmd_client,
241 get => {
242 unixtime => \&_cmd_get_unixtime,
243 },
244 quit => \&_cmd_quit,
245 tunnel => {
246 list => \&_cmd_tunnel_list,
247 show => \&_cmd_tunnel_show,
248 },
249 username => \&_cmd_username,
250 );
251  
252 sub run_command($ $ $)
253 0 ▶{
Subroutine run_command: not called CC=6 SCAR=37.4
Pod documented
254 0 my ($self, $client, $command) = @_;
255  
256 0 ▶ if (!@{$command}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$command;}) 0 0 0
257 0 return $self->client_write_line($client,
258 '500 Invalid empty command');
259 }
260  
261 0 my $handlers = \%cmds;
262 0 while (1) {
263 0 my $cmd = shift @{$command};
264 0 my $c = $handlers->{$cmd};
265  
266 0 ▶ if (!defined $c) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $c) { } 0 0 0
elsif (ref $c eq 'CODE') { } 0 0 0
elsif (ref $c eq 'HASH') { } 0 0 0
267 0 return $self->client_write_line($client,
268 '500 Invalid token: '.$cmd);
269 } elsif (ref $c eq 'CODE') {
270 0 return $c->($self, $client, $cmd, $command);
271 } elsif (ref $c eq 'HASH') {
272 0 ▶ if (!@{$command}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$command;}) 0 0 0
273 0 return $self->client_write_line($client,
274 '500 Need a subcommand after '.$cmd);
275 }
276 0 $handlers = $c;
277 } else {
278 0 return $self->client_write_line($client,
279 '500 Internal server error: unexpected handler '.
280 "for '$cmd': ".ref($c));
281 }
282 }
283 }
284  
285 sub debug($ $)
286 0 ▶{
Subroutine debug: not called CC=2 SCAR=17.9
Pod documented
287 0 my ($self, $msg) = @_;
288  
289 0 ▶ $self->diag->debug($msg) if $self->diag;
Branch: 1 branch
BranchTrueFalseTotal
if $self->diag 0 0 0
290 }
291  
292 1 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
293 1 1;
294 __END__
« SixXS/TIC/Client.pm ↑ Summary SixXS.pm »