blib/lib/POE/Component/Server/Ident.pm

filter: Statementstmt 85.3% 128 / 150 Branchbran 58.3% 28 / 48 Conditioncond 37.5% 9 / 24 MC/DCmcdc 8.3% 1 / 12 Subroutinesub 84.6% 22 / 26 Podpod 100% 7 / 7 totaltotal 73.0% 195 / 267 SCAR 41.7
CC
39
Cov
74%
ident_server_error
20.0
_valid_ports
17.6
spawn
6.0
CRAP 64.7
↑ Summary
1 package POE::Component::Server::Ident;
2  
3 7 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 use POE qw( Wheel::SocketFactory Wheel::ReadWrite Driver::SysRW
7 7 ▶ Filter::Line );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 7 $VERSION = '1.16';
13  
14 sub spawn {
15 5 ▶ my $package = shift;
Subroutine spawn: called CC=6 SCAR=18.0
Pod documented
16 5 my %opts = @_;
17 5 $opts{lc $_} = delete $opts{$_} for keys %opts;
18  
19 5 ▶ $opts{bindport} = 113 unless defined $opts{bindport};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts{"bindport"} 0 5 5
20 5 ▶ $opts{multiple} = 0 unless $opts{multiple};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"multiple"} 4 1 5
21 5 ▶ $opts{timeout} = 60 unless $opts{timeout};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"timeout"} 5 0 5
22 5 ▶ $opts{random} = 0 unless $opts{random};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"random"} 4 1 5
23  
24 5 my $self = bless \%opts, $package;
25  
26 $self->{session_id} = POE::Session->create (
27 object_states => [
28 $self => { _start => '_server_start',
29 'shutdown' => '_server_close',
30 5 map { ( $_ => '_' . $_ ) } qw(accept_new_client accept_failed),
31 },
32 $self => [ qw(register unregister) ],
33 ],
34 )->ID();
35 5 return $self;
36 }
37  
38 sub session_id {
39 0 ▶ return $_[0]->{session_id};
Subroutine session_id: not called CC=1 SCAR=6.9
Pod documented
40 }
41  
42 sub getsockname {
43 5 ▶ my $self = shift;
Subroutine getsockname: called CC=1 SCAR=0.0
Pod documented
44 5 return $self->{listener}->getsockname();
45 }
46  
47 sub _server_start {
48 5 ▶ my ($kernel,$self,$session) = @_[KERNEL,OBJECT,SESSION];
Subroutine _server_start: called CC=4 SCAR=14.5
49 5 $self->{session_id} = $session->ID();
50  
51 5 ▶ $kernel->alias_set( $self->{alias} ) if $self->{alias};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"alias"} 5 0 5
52 5 ▶ $kernel->refcount_increment( $self->{session_id}, __PACKAGE__ ) unless $self->{alias};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"alias"} 0 5 5
53  
54 $self->{listener} = POE::Wheel::SocketFactory->new (
55 BindPort => $self->{bindport},
56 5 ▶ ( $self->{bindaddr} ? (BindAddr => $self->{bindaddr}) : () ),
Branch: 1 branch
BranchTrueFalseTotal
$self->{'bindaddr'} ? : 5 0 5
57 Reuse => 'on',
58 SuccessEvent => 'accept_new_client',
59 FailureEvent => 'accept_failed',
60 );
61 5 undef;
62 }
63  
64 sub _server_close {
65 5 ▶ my ($kernel,$self) = @_[KERNEL,OBJECT];
Subroutine _server_close: called CC=5 SCAR=16.1
66 5 $kernel->alias_remove( $_ ) for $kernel->alias_list();
67 5 ▶ $kernel->refcount_decrement( $self->{session_id}, __PACKAGE__ ) unless $self->{alias};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"alias"} 0 5 5
68 5 $kernel->post( $_, 'client_timeout' ) for %{ $self->{clients} };
69 5 delete $self->{listener};
70 5 $kernel->refcount_decrement( $_, __PACKAGE__ ) for keys %{ $self->{sessions} };
71 5 undef;
72 }
73  
74 sub _accept_new_client {
75 5 ▶ my ($kernel,$self,$socket,$peeraddr,$peerport) = @_[KERNEL,OBJECT,ARG0 .. ARG2];
Subroutine _accept_new_client: called CC=1 SCAR=0.0
76 5 $peeraddr = inet_ntoa($peeraddr);
77  
78 POE::Session->create (
79 object_states => [
80 $self => { _start => '_client_start',
81 _stop => '_client_stop',
82 5 map { ( $_ => '_' . $_ ) } qw(client_input client_error client_done client_timeout client_default),
83 },
84 $self => [ qw(ident_server_reply ident_server_error) ],
85 ],
86 args => [ $socket, $peeraddr, $peerport ],
87 );
88 5 undef;
89 }
90  
91 sub _accept_failed {
92 0 ▶ my ($kernel,$self,$function,$error) = @_[KERNEL,OBJECT,ARG0,ARG2];
Subroutine _accept_failed: not called CC=1 SCAR=6.9
93 0 my $package = ref $self;
94  
95 0 $kernel->call ( $self->{session_id}, 'shutdown' );
96  
97 0 warn "$package: call to $function() failed: $error";
98 0 undef;
99 }
100  
101 sub register {
102 5 ▶ my ($kernel,$self,$sender,$session) = @_[KERNEL,OBJECT,SENDER,SESSION];
Subroutine register: called CC=3 SCAR=11.6
Pod documented
103 5 $sender = $sender->ID();
104 5 $session = $session->ID();
105  
106 5 $self->{sessions}->{ $sender }++;
107 $kernel->refcount_increment( $sender => __PACKAGE__ )
108 5 ▶ if $self->{sessions}->{ $sender } == 1 and $sender ne $session;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"sessions"}{$sender} == 1 and $sender ne $session 5 0 5
Condition33%1/3
$self->{"sessions"}{$sender} == 1 and $sender ne $session
!ll&&!rl&&r
005
MC/DC0%0/2
$self->{"sessions"}{$sender} == 1 and $sender ne $session
$self->{"sessions"}{$sender} == 1 $sender ne $session
Truth table33%1/3
A and B
A= $self->{"sessions"}{$sender} == 1B= $sender ne $session
ABresult
0X0
100
111
109 5 undef;
110 }
111  
112  
113 sub unregister {
114 4 ▶ my ($kernel,$self,$sender,$session) = @_[KERNEL,OBJECT,SENDER,SESSION];
Subroutine unregister: called CC=3 SCAR=12.0
Pod documented
115 4 my $thing = delete $self->{sessions}->{ $sender };
116 4 ▶ $kernel->refcount_decrement( $sender => __PACKAGE__ )
Branch: 1 branch
BranchTrueFalseTotal
if $thing and $sender ne $session 0 4 4
Condition33%1/3
$thing and $sender ne $session
!ll&&!rl&&r
400
MC/DC0%0/2
$thing and $sender ne $session
$thing $sender ne $session
Truth table33%1/3
A and B
A= $thingB= $sender ne $session
ABresult
0X0
100
111
117 if $thing and $sender ne $session;
118 4 return;
119 }
120  
121 sub _client_start {
122 5 ▶ my ($kernel,$session,$self,$socket,$peeraddr,$peerport) = @_[KERNEL,SESSION,OBJECT,ARG0,ARG1,ARG2];
Subroutine _client_start: called CC=2 SCAR=6.9
123 5 my $session_id = $session->ID();
124
125 5 $self->{clients}->{ $session_id }->{PeerAddr} = $peeraddr;
126 5 $self->{clients}->{ $session_id }->{PeerPort} = $peerport;
127  
128 $self->{clients}->{ $session_id }->{readwrite} =
129 POE::Wheel::ReadWrite->new(
130 Handle => $socket,
131 Filter => POE::Filter::Line->new( Literal => "\x0D\x0A" ),
132 InputEvent => 'client_input',
133 ErrorEvent => 'client_error',
134 5 ▶ ( $self->{'multiple'} ? () : ( FlushedEvent => 'client_timeout' ) ),
Branch: 1 branch
BranchTrueFalseTotal
$self->{'multiple'} ? : 1 4 5
135 );
136  
137 # Set a delay to close the connection if we are idle for 60 seconds.
138 5 $kernel->delay ( 'client_timeout' => $self->{'timeout'} );
139 5 undef;
140 }
141  
142 sub _client_stop {
143 5 ▶ my ($kernel,$self,$session) = @_[KERNEL,OBJECT,SESSION];
Subroutine _client_stop: called CC=1 SCAR=0.0
144  
145 5 $kernel->delay ( 'client_timeout' => undef );
146 5 delete $self->{clients}->{ $session->ID };
147 5 undef;
148 }
149  
150 sub _client_input {
151 5 ▶ my ($kernel,$self,$session,$input) = @_[KERNEL,OBJECT,SESSION,ARG0];
Subroutine _client_input: called CC=5 SCAR=16.1
152 5 my $session_id = $session->ID();
153  
154 # Parse what is passed. We want <number>,<number> or nothing.
155  
156 5 ▶ if ( $input =~ /^\s*([0-9]+)\s*,\s*([0-9]+)\s*$/ and _valid_ports($1,$2) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input =~ /^\s*([0-9]+)\s*,\s*([0-9]+)\s*$/ and _valid_ports($1, $2)) { } 4 1 5
Condition66%2/3
$input =~ /^\s*([0-9]+)\s*,\s*([0-9]+)\s*$/ and _valid_ports($1, $2)
!ll&&!rl&&r
104
MC/DC50%1/2
$input =~ /^\s*([0-9]+)\s*,\s*([0-9]+)\s*$/ and _valid_ports($1, $2)
$input =~ /^\s*([0-9]+)\s*,\s*([0-9]+)\s*$/ _valid_ports($1, $2)
Truth table66%2/3
A and B
A= $input =~ /^\s*([0-9]+)\s*,\s*([0-9]+)\s*$/B= _valid_ports($1, $2)
ABresult
0X0
100
111
157 4 my $port1 = $1; my $port2 = $2;
158 4 $self->{clients}->{ $session_id }->{'Port1'} = $port1;
159 4 $self->{clients}->{ $session_id }->{'Port2'} = $port2;
160 # Okay got a sort of valid query. Send it to all interested sessions.
161 4 $kernel->call( $_ => 'identd_request' => $self->{clients}->{ $session_id }->{PeerAddr} => $port1 => $port2 ) for keys %{ $self->{sessions} };
162 4 $kernel->delay ( 'client_default' => 10 );
163 } else {
164 # Client sent us rubbish.
165 1 $self->{clients}->{ $session_id }->{readwrite}->put("0 , 0 : ERROR : INVALID-PORT");
166 }
167 5 ▶ $kernel->delay ( 'client_timeout' => $self->{'timeout'} ) if $self->{'multiple'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"multiple"} 1 4 5
168 5 undef;
169 }
170  
171 sub _client_done {
172 0 ▶ my ($kernel,$self,$session) = @_[KERNEL,OBJECT,SESSION];
Subroutine _client_done: not called CC=1 SCAR=6.9
173 0 $kernel->delay ( 'client_timeout' => undef );
174 0 delete $self->{clients}->{ $session->ID };
175 0 undef;
176 }
177  
178 sub _client_error {
179 1 ▶ my ($kernel,$self,$session) = @_[KERNEL,OBJECT,SESSION];
Subroutine _client_error: called CC=1 SCAR=0.0
180 1 $kernel->delay ( 'client_timeout' => undef );
181 1 delete $self->{clients}->{ $session->ID }->{readwrite};
182 1 undef;
183 }
184  
185 sub _client_timeout {
186 4 ▶ my ($kernel,$self,$session) = @_[KERNEL,OBJECT,SESSION];
Subroutine _client_timeout: called CC=1 SCAR=0.0
187 4 $kernel->delay ( 'client_timeout' => undef );
188 4 $kernel->delay ( 'client_default' => undef );
189 4 delete $self->{clients}->{ $session->ID }->{readwrite};
190 4 undef;
191 }
192  
193 sub _client_default {
194 3 ▶ my ($kernel,$self,$session) = @_[KERNEL,OBJECT,SESSION];
Subroutine _client_default: called CC=6 SCAR=17.9
195 3 my $session_id = $session->ID();
196  
197 3 my $reply = $self->{clients}->{ $session_id }->{'Port1'} . " , " . $self->{clients}->{ $session_id }->{'Port2'};
198 SWITCH: {
199 3 ▶ if ( $self->{'default'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"default"}) 1 2 3
200 1 $reply .= " : USERID : UNIX : " . $self->{'default'};
201 1 last SWITCH;
202 }
203 2 ▶ if ( $self->{'random'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"random"}) 1 1 2
204 1 srand( $session_id * $$ );
205 1 my @numbers;
206 1 push @numbers, int rand (26) for 1 .. 8;
207 1 my $user_id = join '', map { chr($_+97) } @numbers;
208 1 $reply .= " : USERID : UNIX : $user_id";
209 1 last SWITCH;
210 }
211 1 $reply .= " : ERROR : HIDDEN-USER";
212 }
213 3 ▶ $self->{clients}->{ $session_id }->{readwrite}->put($reply) if defined $self->{clients}->{ $session_id }->{readwrite};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"clients"}{$session_id}{"readwrite"} 3 0 3
214 3 ▶ $kernel->delay ( 'client_timeout' => $self->{'timeout'} ) if $self->{'multiple'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"multiple"} 0 3 3
215 3 undef;
216 }
217  
218 sub ident_server_reply {
219 1 ▶ my ($kernel,$self,$session) = @_[KERNEL,OBJECT,SESSION];
Subroutine ident_server_reply: called CC=4 SCAR=14.2
Pod documented
220 1 my $session_id = $session->ID();
221  
222 1 my ($opsys,$userid) = @_[ARG0 .. ARG1];
223  
224 1 ▶ $opsys = "UNIX" unless defined ( $opsys );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opsys 0 1 1
225  
226 1 my $reply = $self->{clients}->{ $session_id }->{'Port1'} . " , " . $self->{clients}->{ $session_id }->{'Port2'} . " : USERID : " . $opsys . " : " . $userid;
227  
228 1 ▶ $self->{clients}->{ $session_id }->{readwrite}->put($reply) if $self->{clients}->{ $session_id }->{readwrite};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"clients"}{$session_id}{"readwrite"} 1 0 1
229 1 ▶ $kernel->delay ( 'client_timeout' => $self->{'timeout'} ) if $self->{'multiple'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"multiple"} 0 1 1
230 1 $kernel->delay ( 'client_default' => undef );
231 1 undef;
232 }
233  
234 sub ident_server_error {
235 0 ▶ my ($kernel,$self,$session,$error_type) = @_[KERNEL,OBJECT,SESSION,ARG0];
Subroutine ident_server_error: not called CC=4 SCAR=30.0
Pod documented
236 0 my $session_id = $session->ID();
237 0 $error_type = uc $error_type;
238  
239 0 ▶ unless ( grep {$_ eq $error_type} qw(INVALID-PORT NO-USER HIDDEN-USER UNKNOWN-ERROR) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq $error_type;} "INVALID-PORT", "NO-USER", "HIDDEN-USER", "UNKNOWN-ERROR") 0 0 0
240 0 $error_type = 'UNKNOWN-ERROR';
241 }
242  
243 0 my $reply = $self->{clients}->{ $session_id }->{'Port1'} . " , " . $self->{clients}->{ $session_id }->{'Port2'} . " : ERROR : " . $error_type;
244  
245 0 ▶ $self->{clients}->{ $session_id }->{readwrite}->put($reply) if $self->{clients}->{ $session_id }->{readwrite};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"clients"}{$session_id}{"readwrite"} 0 0 0
246 0 ▶ $kernel->delay ( 'client_timeout' => $self->{'timeout'} ) if $self->{'multiple'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"multiple"} 0 0 0
247 0 $kernel->delay ( 'client_default' => undef );
248 0 undef;
249 }
250  
251 sub _valid_ports {
252 4 ▶ my ($port1,$port2) = @_;
Subroutine _valid_ports: called CC=7 SCAR=28.7
253  
254 4 ▶ return 1 if ( defined ( $port1 ) and defined ( $port2 ) ) and ( $port1 >= 1 and $port1 <= 65535 ) and ( $port2 >= 1 and $port2 <= 65535 );
Branch: 1 branch
BranchTrueFalseTotal
if defined $port1 and defined $port2 and ($port1 >= 1 and $port1 <= 65535) and ($port2 >= 1 and $port2 <= 65535) 4 0 4
Condition33%5/15
defined $port1 and defined $port2 and ($port1 >= 1 and $port1 <= 65535) and ($port2 >= 1 and $port2 <= 65535)
!ll&&!rl&&r
004
defined $port1 and defined $port2 and ($port1 >= 1 and $port1 <= 65535)
!ll&&!rl&&r
004
defined $port1 and defined $port2
!ll&&!rl&&r
004
$port1 >= 1 and $port1 <= 65535
!ll&&!rl&&r
004
$port2 >= 1 and $port2 <= 65535
!ll&&!rl&&r
004
MC/DC0%0/6
defined $port1 and defined $port2 and ($port1 >= 1 and $port1 <= 65535) and ($port2 >= 1 and $port2 <= 65535)
defined $port1 defined $port2 $port1 >= 1 $port1 <= 65535 $port2 >= 1 $port2 <= 65535
Truth table14%1/7
A and B and C and D and E and F
A= defined $port1B= defined $port2C= $port1 >= 1D= $port1 <= 65535E= $port2 >= 1F= $port2 <= 65535
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
255 0 return 0;
256 }
257  
258 7 1;
259  
260 =head1 NAME
261  
262 POE::Component::Server::Ident - A POE component that provides non-blocking ident services to your sessions.
263  
264 =head1 SYNOPSIS
265  
266 use strict;
267 use warnings;
268 use POE qw(Component::Server::Ident);
269  
270 POE::Component::Server::Ident->spawn ( Alias => 'Ident-Server' );
271  
272 POE::Session->create (
273 package_states => [
274 'main' => [qw(_start identd_request)],
275 ],
276 );
277  
278 $poe_kernel->run();
279 exit 0;
280  
281 sub _start {
282 $poe_kernel->post( 'Ident-Server' => 'register' );
283 undef;
284 }
285  
286  
287 sub identd_request {
288 my ($kernel,$sender,$peeraddr,$port1,$port2) = @_[KERNEL,SENDER,ARG0,ARG1,ARG2];
289 my ($val1,$val2);
290 $val1 = $val2 = int(rand(99999));
291 $val1 =~ tr/0-9/A-Z/;
292 $kernel->call ( $sender => ident_server_reply => 'OTHER' => "$val1$val2" );
293 undef;
294 }
295  
296 =head1 DESCRIPTION
297  
298 POE::Component::Server::Ident is a L<POE> component that provides
299 a non-blocking Identd for other components and POE sessions.
300  
301 Spawn the component, give it an an optional lias and it will sit there waiting for Ident clients to connect.
302 Register with the component to receive ident events. The component will listen for client connections.
303 A valid ident request made by a client will result in an 'identd_server' event being sent to your
304 session. You may send back 'ident_server_reply' or 'ident_server_error' depending on what the client
305 sent.
306  
307 The component will automatically respond to the client requests with 'ERROR : HIDDEN-USER' if your
308 sessions do not send a respond within a 10 second timeout period. This can be adjusted with 'Random'
309 and 'Default' options to spawn().
310  
311 =head1 CONSTRUCTOR
312  
313 =over
314  
315 =item C<spawn>
316  
317 Takes a number of arguments:
318  
319 'Alias', a kernel alias to address the component with;
320 'BindAddr', the IP address that the component should bind to,
321 defaults to INADDR_ANY;
322 'BindPort', the port that the component will bind to, default is 113;
323 'Multiple', specify whether the component should allow multiple ident queries
324 from clients by setting this to 1, default is 0 which terminates
325 client connections after a response has been sent;
326 'TimeOut', this is the idle timeout on client connections, default
327 is 60 seconds, accepts values between 60 and 180 seconds.
328 'Default', provide a default userid to return if your sessions don't provide a
329 response.
330 'Random', the component will generate a random userid string if your sessions
331 don't provide a response.
332
333  
334 =back
335  
336 =head1 METHODS
337  
338 =over
339  
340 =item C<session_id>
341  
342 Retrieve the component's POE session ID.
343  
344 =item C<getsockname>
345  
346 Access to the L<POE::Wheel::SocketFactory> method of the underlying listening socket.
347  
348 =back
349  
350 =head1 INPUT
351  
352 The component accepts the following events:
353  
354 =over
355  
356 =item C<register>
357  
358 Takes no arguments. This registers your session with the component. The component will then send you
359 'identd_request' events when clients make valid ident requests. See below.
360  
361 =item C<unregister>
362  
363 Takes no arguments. This unregisters your session with the component.
364  
365 =item C<ident_server_reply>
366  
367 Takes two arguments, the first is the 'opsys' field of the ident response, the second is the 'userid'.
368  
369 =item C<ident_server_error>
370  
371 Takes one argument, the error to return to the client.
372  
373 =item C<shutdown>
374  
375 Terminates the component. The listener is closed and all current client connections are disconnected.
376  
377 =back
378  
379 =head1 OUTPUT
380  
381 The component will send the following events:
382  
383 =over
384  
385 =item C<identd_request>
386  
387 Sent by the component to 'registered' sessions when a client makes a valid ident request. ARG0 is
388 the IP address of the client, ARG1 and ARG2 are the ports as specified in the ident request. You
389 can use the 'ident_server_reply' and 'ident_server_error' to respond to the client appropriately. Please
390 note, that you send these responses to $_[SENDER] not the kernel alias of the component.
391  
392 =back
393  
394 =head1 AUTHOR
395  
396 Chris C<BinGOs> Williams, E<lt>chris@bingosnet.co.ukE<gt>
397  
398 =head1 LICENSE
399  
400 Copyright E<copy> Chris Williams
401  
402 This module may be used, modified, and distributed under the same terms as Perl itself. Please see the license that came with your Perl distribution for details.
403  
404 =head1 SEE ALSO
405  
406 L<POE>
407  
408 RFC 1413 L<http://www.faqs.org/rfcs/rfc1413.html>
409  
410 =cut
↑ Summary