Component/Server/TacacsPlus.pm

filter: Statementstmt 86.6% 13 / 15 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 90.0% 18 / 20 SCAR 0.0
CC
1
Cov
87%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Filter/TacacsPlus.pm ↑ Summary
1 package POE::Component::Server::TacacsPlus;
2  
3 =head1 NAME
4  
5 POE::Component::Server::TacacsPlus - POE Tacacs+ server component
6  
7 =head1 SYNOPSIS
8  
9 use POE::Component::Server::TacacsPlus;
10 use Net::TacacsPlus::Constants;
11
12 POE::Component::Server::TacacsPlus->spawn(
13 'server_port' => 4949,
14 'key' => 'secret',
15 'handler_for' => {
16 TAC_PLUS_AUTHEN() => {
17 TAC_PLUS_AUTHEN_TYPE_PAP() => \&check_pap_authentication,
18 },
19 },
20 );
21
22 POE::Kernel->run();
23
24 sub check_pap_authentication {
25 my $username = shift;
26 my $password = shift;
27
28 return 1 if $password = $username.'123';
29 return 0;
30 }
31  
32 =head1 DESCRIPTION
33  
34 This component will listen on $server_port for Tacacs+ client reqests and
35 dispatch them to the local functions handlers.
36  
37 Currently only PAP requests can be dispatched as this is HIGHLY experimental
38 and more like idea then functional code even that the pap is working fine.
39  
40 I'm looking forward to you comments and suggestions until i'll invest more time
41 on it.
42  
43 =cut
44  
45 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 1 ▶use Socket qw(inet_ntoa);
Subroutine BEGIN: called CC=1 SCAR=0.0
49 1 use POE qw{
50 Wheel::SocketFactory
51 Wheel::ReadWrite
52 Filter::TacacsPlus
53 Driver::SysRW
54 1 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
55 1 ▶use Log::Log4perl qw(:nowarn :easy :no_extra_logdie_message);
Subroutine BEGIN: called CC=1 SCAR=2.6
56 use Net::TacacsPlus::Constants;
57 use Carp::Clan;
58  
59 our $VERSION = '1.10_01';
60 our $SERVER_PORT = 49;
61  
62  
63 =head1 FUNCTIONS
64  
65 =head2 spawn
66  
67 Accepts following parameters:
68  
69 key : tacacs secret key
70 server_port : port on which to listen (optional) - default 49
71 handler_for : hash ref of handlers. keies are one of TAC_PLUS_AUTHEN, TAC_PLUS_AUTHOR and TAC_PLUS_ACCT
72  
73 =cut
74  
75 sub spawn {
76 my $class = shift;
77 my $heap = { @_ };
78
79 croak 'pass at least one type handler' if not exists $heap->{'handler_for'};
80 croak 'handler_for must be hash ref' if ref $heap->{'handler_for'} ne 'HASH';
81
82 POE::Session->create(
83 inline_states => {
84 _start => \&server_start,
85 accept_new_client => \&accept_new_client,
86 accept_failed => \&accept_failed,
87 _stop => \&server_stop,
88 },
89 heap => $heap,
90 );
91  
92 }
93  
94  
95 =head2 server_start
96  
97 Component _start function.
98  
99 =cut
100  
101 sub server_start {
102 my $heap = $_[HEAP];
103
104 $SERVER_PORT = $heap->{'server_port'} if exists $heap->{'server_port'};
105
106 $heap->{'listener'} = new POE::Wheel::SocketFactory(
107 BindPort => $SERVER_PORT,
108 Reuse => 'yes',
109 SuccessEvent => 'accept_new_client',
110 FailureEvent => 'accept_failed',
111 );
112 INFO 'SERVER: Started listening on port ', $SERVER_PORT;
113 }
114  
115  
116 =head2 server_stop
117  
118 Component _stop function.
119  
120 =cut
121  
122 sub server_stop {
123 INFO "SERVER: Stopped.\n";
124 }
125  
126  
127 =head2 accept_new_client
128  
129 On client connect setup function. Will setup POE::Session
130 to handle client input using child_* function.
131  
132 =cut
133  
134 sub accept_new_client {
135 my $heap = $_[HEAP];
136 my $socket = $_[ARG0];
137 my $peer_addr = $_[ARG1];
138 my $peer_port = $_[ARG2];
139
140 $peer_addr = inet_ntoa($peer_addr);
141  
142 POE::Session->create(
143 inline_states => {
144 _start => \&child_start,
145 _stop => \&child_stop,
146 child_input => \&child_input,
147 child_done => \&child_done,
148 child_error => \&child_error,
149 },
150 args => [ $socket, $peer_addr, $peer_port ],
151 heap => {
152 'key' => $heap->{'key'},
153 'handler_for' => $heap->{'handler_for'},
154 },
155 );
156 DEBUG 'SERVER: Got connection from '.$peer_addr.':'.$peer_port;
157 }
158  
159  
160 =head2 accept_failed
161  
162 Tidy up and print out error message if accept failed.
163  
164 =cut
165  
166 sub accept_failed {
167 my $function = $_[ARG0];
168 my $error = $_[ARG2];
169 my $heap = $_[HEAP];
170  
171 delete $heap->{'listener'};
172 ERROR 'SERVER: call to '.$function.'() failed: '.$error.'.';
173 }
174  
175  
176 =head2 child_start
177  
178 Setup POE::Wheel::ReadWrite with POE::Filter::TacacsPlus so that
179 child_input will receive directly Net::TacacsPlus::Packet objects
180 as input.
181  
182 =cut
183  
184 sub child_start {
185 my $heap = $_[HEAP];
186 my $socket = $_[ARG0];
187 my $peer_addr = $_[ARG1];
188 my $peer_port = $_[ARG2];
189  
190 $heap->{'peername'} = $peer_addr.':'.$peer_port;
191  
192 $heap->{'readwrite'} = new POE::Wheel::ReadWrite (
193 Handle => $socket,
194 Driver => new POE::Driver::SysRW(),
195 Filter => new POE::Filter::TacacsPlus(
196 'key' => $heap->{'key'}
197 ),
198 InputEvent => 'child_input',
199 ErrorEvent => 'child_error',
200 );
201  
202 DEBUG 'CHILD: Connected to '.$heap->{'peername'};
203 }
204  
205  
206 =head2 child_stop
207  
208 Just print out the debug message that the child is finished.
209  
210 =cut
211  
212 sub child_stop {
213 DEBUG "CHILD: Stopped.\n";
214 }
215  
216  
217 =head2 child_input
218  
219 Process incomming Net::TacacsPlus::Packet and call propper
220 handler.
221  
222 =cut
223  
224 sub child_input {
225 my $packet = $_[ARG0];
226 my $heap = $_[HEAP];
227 my $kernel = $_[KERNEL];
228  
229 my $reply;
230  
231 DEBUG "CHILD: Got input from peer";
232
233 my $packet_type = $packet->type;
234 DEBUG "packet type> ".$packet_type;
235  
236 # check if we have handler for authentication
237 my $handler = $heap->{'handler_for'}->{$packet_type};
238 if (not defined $handler ) {
239 $kernel->yield('child_error', 'packet processing', undef, 'no handler for packet type '.$packet_type);
240 return;
241 }
242  
243 # handle authentication requests
244 if ($packet_type == TAC_PLUS_AUTHEN) {
245 # check if we have handler for current authentification type
246 my $authen_type = $packet->body->authen_type;
247 $handler = $handler->{$authen_type};
248 if (not defined $handler) {
249 $kernel->yield('child_error', 'authentication', undef, 'no handler for authen_type '.$authen_type.' (wrong key?)');
250 return;
251 };
252  
253 # construct default reply packet
254 $reply = Net::TacacsPlus::Packet->new(
255 'version' => $packet->version,
256 'type' => TAC_PLUS_AUTHEN,
257 'seq_no' => $packet->seq_no + 1,
258 'session_id' => $packet->session_id,
259  
260 'key' => $heap->{'key'},
261 'authen_type' => $authen_type,
262 'flags' => 0,
263 'status' => TAC_PLUS_AUTHEN_STATUS_FAIL,
264 );
265
266 # call handler and update returned status according to the reply
267 if ($handler->($packet->body->user, $packet->body->data)) {
268 $reply->status(TAC_PLUS_AUTHEN_STATUS_PASS);
269 }
270 else {
271 $reply->status(TAC_PLUS_AUTHEN_STATUS_FAIL);
272 }
273 }
274 else {
275 $kernel->yield('child_error', 'packet processing', undef, ' unsupported handler for packet type '.$packet_type);
276 return;
277 }
278  
279 $heap->{'readwrite'}->put($reply);
280 }
281  
282  
283 =head2 child_done
284  
285 Cleanup after child is done.
286  
287 =cut
288  
289 sub child_done {
290 my $heap = $_[HEAP];
291  
292 delete $heap->{'readwrite'};
293 DEBUG "CHILD: disconnected from ", $heap->{'peername'};
294 }
295  
296  
297 =head2 child_error
298  
299 Print out error and do cleanup.
300  
301 =cut
302  
303 sub child_error {
304 my $operation = $_[ARG0];
305 my $error_num = $_[ARG1] || '';
306 my $error_msg = $_[ARG2];
307 my $heap = $_[HEAP];
308
309 delete $heap->{'readwrite'};
310 ERROR 'failed '.$operation.' ('.$error_num.') - '.$error_msg if $error_msg;
311 }
312  
313 1;
314  
315 =head1 SEE ALSO
316  
317 tac-rfc.1.78.txt
318  
319 Complete server script C<Net-TacacsPlus/examples/server.pl>.
320  
321 =cut
« Filter/TacacsPlus.pm ↑ Summary