| 1 | package AnyEvent::Radius::Server; | ||||||||||||||||||||||||||||
| 2 | # AnyEvent-based radius server | ||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use AnyEvent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use AnyEvent::Handle::UDP; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use base qw(Class::Accessor::Fast); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | __PACKAGE__->mk_accessors(qw(handler packer)); | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Data::Radius v1.2.8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Data::Radius::Constants qw(:all); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Data::Radius::Dictionary (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use Data::Radius::Packet (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use constant { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 1 | READ_TIMEOUT_SEC => 5, | |||||||||||||||||||||||||||
| 18 | WRITE_TIMEOUT_SEC => 5, | ||||||||||||||||||||||||||||
| 19 | RADIUS_PORT => 1812, | ||||||||||||||||||||||||||||
| 20 | 1 | }; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 1 | my %DEFAUL_REPLY = ( | |||||||||||||||||||||||||||
| 23 | &ACCESS_REQUEST => ACCESS_REJECT, | ||||||||||||||||||||||||||||
| 24 | &ACCOUNTING_REQUEST => ACCOUNTING_RESPONSE, | ||||||||||||||||||||||||||||
| 25 | &DISCONNECT_REQUEST => DISCONNECT_REJECT, | ||||||||||||||||||||||||||||
| 26 | &COA_REQUEST => COA_REJECT, | ||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | # new 'server' | ||||||||||||||||||||||||||||
| 30 | # args: | ||||||||||||||||||||||||||||
| 31 | # ip | ||||||||||||||||||||||||||||
| 32 | # port | ||||||||||||||||||||||||||||
| 33 | # secret | ||||||||||||||||||||||||||||
| 34 | # dictionary | ||||||||||||||||||||||||||||
| 35 | #- callbacks: | ||||||||||||||||||||||||||||
| 36 | # on_read | ||||||||||||||||||||||||||||
| 37 | # on_read_raw | ||||||||||||||||||||||||||||
| 38 | # on_wrong_request | ||||||||||||||||||||||||||||
| 39 | # on_error | ||||||||||||||||||||||||||||
| 40 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 41 | 0 | my ($class, %h) = @_; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 0 | ▶ | die "No IP argument" if (! $h{ip}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | # either pre-created packer obect, or need radius secret to create new one | ||||||||||||||||||||||||||||
| 45 | # dictionary is optional | ||||||||||||||||||||||||||||
| 46 | 0 | ▶ | die "No radius secret" if (! $h{packer} && ! $h{secret}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$h{"packer"} or $h{"secret"}
MC/DC0%0/2
$h{"packer"} or $h{"secret"}
$h{"packer"}
$h{"secret"}
Truth table0%0/3
A or B
A= $h{"packer"}B= $h{"secret"}
| |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 0 | my $obj = bless {}, $class; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 0 | ▶ | my $on_read_cb = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||
| 51 | 0 | my ($data, $handle, $from) = @_; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 0 | ▶ | if ($h{on_read_raw}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | # dump raw data | ||||||||||||||||||||||||||||
| 55 | 0 | $h{on_read_raw}->($obj, $data, $from); | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | # how to decoded $from | ||||||||||||||||||||||||||||
| 59 | # my($port, $host) = AnyEvent::Socket::unpack_sockaddr($from); | ||||||||||||||||||||||||||||
| 60 | # my $ip = format_ipv4($host); | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 0 | my ($type, $req_id, $authenticator, $av_list) = $obj->packer()->parse($data); | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 0 | ▶ | if (! $obj->packer()->is_request($type)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | # we expect only requests in server | ||||||||||||||||||||||||||||
| 66 | 0 | ▶ | if ($h{on_wrong_request}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 0 | $h{on_wrong_request}->($obj, { | |||||||||||||||||||||||||||
| 68 | type => $type, | ||||||||||||||||||||||||||||
| 69 | request_id => $req_id, | ||||||||||||||||||||||||||||
| 70 | av_list => $av_list, | ||||||||||||||||||||||||||||
| 71 | # from is sockaddr binary data | ||||||||||||||||||||||||||||
| 72 | from => $from, | ||||||||||||||||||||||||||||
| 73 | }); | ||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | # Do not reply | ||||||||||||||||||||||||||||
| 77 | 0 | warn "Ignore wrong request type " . $type; | |||||||||||||||||||||||||||
| 78 | return | ||||||||||||||||||||||||||||
| 79 | 0 | } | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 0 | my ($reply_type, $reply_av_list) = (); | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 0 | ▶ | if($h{on_read}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | # custom-reply | ||||||||||||||||||||||||||||
| 85 | 0 | ($reply_type, $reply_av_list) = $h{on_read}->($obj, { | |||||||||||||||||||||||||||
| 86 | type => $type, | ||||||||||||||||||||||||||||
| 87 | request_id => $req_id, | ||||||||||||||||||||||||||||
| 88 | av_list => $av_list, | ||||||||||||||||||||||||||||
| 89 | # from is sockaddr binary data | ||||||||||||||||||||||||||||
| 90 | from => $from, | ||||||||||||||||||||||||||||
| 91 | }); | ||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 0 | ▶ | if (! $reply_type) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | # reject by default | ||||||||||||||||||||||||||||
| 96 | 0 | $reply_type = $DEFAUL_REPLY{ $type }; | |||||||||||||||||||||||||||
| 97 | 0 | $reply_av_list = [{Name => 'Reply-Message', Value => 'Default rule: reject'}]; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 0 | my ($reply, $r_id, $r_auth) = $obj->packer()->build( | |||||||||||||||||||||||||||
| 101 | type => $reply_type, | ||||||||||||||||||||||||||||
| 102 | av_list => $reply_av_list, | ||||||||||||||||||||||||||||
| 103 | authenticator => $authenticator, | ||||||||||||||||||||||||||||
| 104 | request_id => $req_id, | ||||||||||||||||||||||||||||
| 105 | ); | ||||||||||||||||||||||||||||
| 106 | 0 | ▶ | if(! $reply) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 0 | warn "Failed to build reply"; | |||||||||||||||||||||||||||
| 108 | return | ||||||||||||||||||||||||||||
| 109 | 0 | } | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 0 | $obj->handler()->push_send($reply, $from); | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 0 | return; | |||||||||||||||||||||||||||
| 114 | 0 | }; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | # low-level socket errors | ||||||||||||||||||||||||||||
| 117 | 0 | ▶ | my $on_error_cb = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 118 | 0 | my ($handle, $fatal, $error) = @_; | |||||||||||||||||||||||||||
| 119 | 0 | ▶ | if ($h{on_error}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 0 | $h{on_error}->($obj, $error); | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | else { | ||||||||||||||||||||||||||||
| 123 | 0 | warn "Error occured: $error"; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | 0 | }; | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | my $server = AnyEvent::Handle::UDP->new( | ||||||||||||||||||||||||||||
| 128 | 0 | ▶ | bind => [$h{ip}, $h{port} // RADIUS_PORT ], | ||||||||||||||||||||||||||
|
Condition0%0/2
$h{"port"} // 1812
MC/DC0%0/1
$h{"port"} // 1812
$h{"port"}
Truth table0%0/2
A
A= $h{"port"}
| |||||||||||||||||||||||||||||
| 129 | on_recv => $on_read_cb, | ||||||||||||||||||||||||||||
| 130 | on_error => $on_error_cb, | ||||||||||||||||||||||||||||
| 131 | ); | ||||||||||||||||||||||||||||
| 132 | 0 | $obj->handler($server); | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | # allow to pass custom object | ||||||||||||||||||||||||||||
| 135 | 0 | ▶ | my $packer = $h{packer} || Data::Radius::Packet->new(dict => $h{dictionary}, secret => $h{secret}); | ||||||||||||||||||||||||||
|
Condition0%0/3
$h{'packer'} || 'Data::Radius::Packet'->new('dict', $h{'dictionary'}, 'secret', $h{'secret'})
MC/DC0%0/2
$h{'packer'} || 'Data::Radius::Packet'->new('dict', $h{'dictionary'}, 'secret', $h{'secret'})
$h{'packer'}
'Data::Radius::Packet'->new('dict', $h{'dictionary'}, 'secret', $h{'secret'})
Truth table0%0/3
A || B
A= $h{'packer'}B= 'Data::Radius::Packet'->new('dict', $h{'dictionary'}, 'secret', $h{'secret'})
| |||||||||||||||||||||||||||||
| 136 | 0 | $obj->packer($packer); | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 0 | return $obj; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 0 | ▶ | sub load_dictionary { | ||||||||||||||||||||||||||
|
Subroutine
load_dictionary: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 142 | 0 | my ($class, $path) = @_; | |||||||||||||||||||||||||||
| 143 | 0 | my $dict = Data::Radius::Dictionary->load_file($path); | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 0 | ▶ | if(ref($class)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 0 | $class->packer()->dict($dict); | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 0 | return $dict; | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 1 | 1; | |||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | __END__ | ||||||||||||||||||||||||||||