« blib/lib/Lemonldap/NG/Portal/UserDB/OpenIDConnect.pm
↑ Summary
blib/lib/Lemonldap/NG/Portal/Lib/Slave.pm »
| 1 | package Lemonldap::NG::Portal::Lib::Radius; | ||||||||||||||||||||||
| 2 | 4 | ▶ | use Authen::Radius; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 3 | |||||||||||||||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 5 | 4 | ▶ | use Mouse; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 6 | |||||||||||||||||||||||
| 7 | 4 | our $VERSION = '2.18.0'; | |||||||||||||||||||||
| 8 | |||||||||||||||||||||||
| 9 | # INITIALIZATION | ||||||||||||||||||||||
| 10 | |||||||||||||||||||||||
| 11 | 4 | has radius => ( is => 'rw' ); | |||||||||||||||||||||
| 12 | 3 | ▶ | has req_attributes_rules => ( is => 'rw', default => sub { {} } ); | ||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 13 | 4 | has radius_server => ( is => 'rw' ); | |||||||||||||||||||||
| 14 | 4 | has radius_secret => ( is => 'rw' ); | |||||||||||||||||||||
| 15 | 4 | has radius_timeout => ( is => 'rw' ); | |||||||||||||||||||||
| 16 | 4 | has radius_req_attribute_config => ( is => 'rw' ); | |||||||||||||||||||||
| 17 | 4 | has radius_dictionary => ( is => 'rw' ); | |||||||||||||||||||||
| 18 | 4 | has radius_msgauth => ( is => 'rw' ); | |||||||||||||||||||||
| 19 | 4 | has modulename => ( is => 'rw' ); | |||||||||||||||||||||
| 20 | 4 | has p => ( is => 'rw' ); | |||||||||||||||||||||
| 21 | 4 | has logger => ( is => 'rw' ); | |||||||||||||||||||||
| 22 | |||||||||||||||||||||||
| 23 | 3 | ▶ | sub BUILD { | ||||||||||||||||||||
|
Subroutine
BUILD: called CC=6 SCAR=20.5
Pod
documented
| |||||||||||||||||||||||
| 24 | 3 | my $self = shift; | |||||||||||||||||||||
| 25 | |||||||||||||||||||||||
| 26 | 3 | ▶ | if ( $self->radius_dictionary ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 27 | |||||||||||||||||||||||
| 28 | # required to be able to resolve names and values | ||||||||||||||||||||||
| 29 | # default to /etc/raddb/dictionary ( same as Authen::Radius library ). | ||||||||||||||||||||||
| 30 | 0 | ▶ | if ( -r $self->radius_dictionary ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 31 | 0 | Authen::Radius->load_dictionary( $self->radius_dictionary ); | |||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||
| 33 | else { | ||||||||||||||||||||||
| 34 | # log an error, avoid server error if missing. | ||||||||||||||||||||||
| 35 | 0 | $self->logger->error( | |||||||||||||||||||||
| 36 | "Radius library resolution of attribute names" | ||||||||||||||||||||||
| 37 | . " requires to set a dictionary in " | ||||||||||||||||||||||
| 38 | . $self->radius_dictionary | ||||||||||||||||||||||
| 39 | . ", this file could not be read." ); | ||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||
| 42 | |||||||||||||||||||||||
| 43 | 3 | ▶ | my $attribute_config = $self->radius_req_attribute_config || {}; | ||||||||||||||||||||
|
Condition50%1/2
$self->radius_req_attribute_config || {}
MC/DC0%0/1
$self->radius_req_attribute_config || {}
$self->radius_req_attribute_config
Truth table50%1/2
A
A= $self->radius_req_attribute_config
| |||||||||||||||||||||||
| 44 | 3 | ▶ | while ( my ( $attr, $rule ) = each %{$attribute_config} ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 45 | 4 | my $compiled_rule = $self->p->buildRule( $rule, | |||||||||||||||||||||
| 46 | $self->modulename . " request attribute" ); | ||||||||||||||||||||||
| 47 | 4 | ▶ | if ($compiled_rule) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 48 | 4 | $self->req_attributes_rules->{$attr} = $compiled_rule; | |||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||
| 51 | 3 | return 1; | |||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||
| 53 | |||||||||||||||||||||||
| 54 | 0 | ▶ | sub _get_radius { | ||||||||||||||||||||
|
Subroutine
_get_radius: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||
| 55 | 0 | my ($self) = @_; | |||||||||||||||||||||
| 56 | |||||||||||||||||||||||
| 57 | # Cache radius object in between requests | ||||||||||||||||||||||
| 58 | 0 | my $radius = $self->radius; | |||||||||||||||||||||
| 59 | 0 | ▶ | return $radius if $radius; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 60 | |||||||||||||||||||||||
| 61 | 0 | foreach (qw(radius_server radius_secret)) { | |||||||||||||||||||||
| 62 | 0 | ▶ | unless ( $self->$_ ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 63 | 0 | $self->logger->error( | |||||||||||||||||||||
| 64 | $self->modulename . ": missing \"$_\" parameter, aborting" ); | ||||||||||||||||||||||
| 65 | 0 | return; | |||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||
| 68 | |||||||||||||||||||||||
| 69 | 0 | my @server_list = split /[,\s]+/, $self->radius_server; | |||||||||||||||||||||
| 70 | 0 | my $first_server = $server_list[0]; | |||||||||||||||||||||
| 71 | 0 | ▶ | $self->logger->error( $self->modulename . ': connection to server failed' ) | ||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||
| 72 | unless ( | ||||||||||||||||||||||
| 73 | $self->radius( | ||||||||||||||||||||||
| 74 | Authen::Radius->new( | ||||||||||||||||||||||
| 75 | Host => $first_server, | ||||||||||||||||||||||
| 76 | Secret => $self->radius_secret, | ||||||||||||||||||||||
| 77 | ( $self->radius_msgauth ? ( Rfc3579MessageAuth => 1 ) : () ), | ||||||||||||||||||||||
| 78 | ( | ||||||||||||||||||||||
| 79 | $self->radius_timeout | ||||||||||||||||||||||
| 80 | ? ( TimeOut => $self->radius_timeout ) | ||||||||||||||||||||||
| 81 | : () | ||||||||||||||||||||||
| 82 | ), | ||||||||||||||||||||||
| 83 | ( @server_list > 1 ? ( NodeList => \@server_list ) : () ), | ||||||||||||||||||||||
| 84 | ) | ||||||||||||||||||||||
| 85 | ) | ||||||||||||||||||||||
| 86 | ); | ||||||||||||||||||||||
| 87 | |||||||||||||||||||||||
| 88 | 0 | return $self->radius; | |||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||
| 90 | |||||||||||||||||||||||
| 91 | 0 | ▶ | sub _check_pwd_radius { | ||||||||||||||||||||
|
Subroutine
_check_pwd_radius: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||
| 92 | 0 | my ( $self, @attributes ) = @_; | |||||||||||||||||||||
| 93 | |||||||||||||||||||||||
| 94 | 0 | my $radius = $self->_get_radius; | |||||||||||||||||||||
| 95 | 0 | ▶ | unless ($radius) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 96 | 0 | return; | |||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||
| 98 | |||||||||||||||||||||||
| 99 | 0 | my $nas = eval { $radius->{'sock'}->sockhost() }; | |||||||||||||||||||||
| 100 | 0 | $radius->clear_attributes; | |||||||||||||||||||||
| 101 | 0 | ▶ | $radius->add_attributes( @attributes, | ||||||||||||||||||||
|
Condition0%0/2
$nas || '127.0.0.1'
MC/DC0%0/1
$nas || '127.0.0.1'
$nas
Truth table0%0/2
A
A= $nas
| |||||||||||||||||||||||
| 102 | { Name => 4, Value => $nas || '127.0.0.1', Type => 'ipaddr' } ); | ||||||||||||||||||||||
| 103 | 0 | $radius->send_packet(Authen::Radius::ACCESS_REQUEST); | |||||||||||||||||||||
| 104 | 0 | my $rcv = $radius->recv_packet(); | |||||||||||||||||||||
| 105 | |||||||||||||||||||||||
| 106 | # Authen::Radius does not handle retrying automatically in | ||||||||||||||||||||||
| 107 | # failover scenarios | ||||||||||||||||||||||
| 108 | 0 | ▶ | if ( $radius->get_error eq "ETIMEOUT" ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 109 | 0 | $self->logger->warn( | |||||||||||||||||||||
| 110 | "Radius request has timed out, retrying on all configured hosts"); | ||||||||||||||||||||||
| 111 | 0 | $radius->send_packet( Authen::Radius::ACCESS_REQUEST, 1 ); | |||||||||||||||||||||
| 112 | 0 | $rcv = $radius->recv_packet(); | |||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||
| 114 | |||||||||||||||||||||||
| 115 | 0 | ▶ | unless ( defined($rcv) ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 116 | 0 | $self->logger->error( | |||||||||||||||||||||
| 117 | "Error contacting Radius server: " . $radius->strerror ); | ||||||||||||||||||||||
| 118 | 0 | return; | |||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||
| 120 | return { | ||||||||||||||||||||||
| 121 | 0 | result => ( $rcv == Authen::Radius::ACCESS_ACCEPT ), | |||||||||||||||||||||
| 122 | attributes => | ||||||||||||||||||||||
| 123 | $self->_parse_attributes( $self->radius->get_attributes() ), | ||||||||||||||||||||||
| 124 | }; | ||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||
| 126 | |||||||||||||||||||||||
| 127 | 0 | ▶ | sub _parse_attributes { | ||||||||||||||||||||
|
Subroutine
_parse_attributes: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||
| 128 | 0 | my ( $self, @recv_attributes ) = @_; | |||||||||||||||||||||
| 129 | 0 | my %attribute_result; | |||||||||||||||||||||
| 130 | |||||||||||||||||||||||
| 131 | 0 | foreach my $a (@recv_attributes) { | |||||||||||||||||||||
| 132 | $self->logger->debug( 'Radius attribute ' | ||||||||||||||||||||||
| 133 | . 'attrName=' | ||||||||||||||||||||||
| 134 | . $a->{AttrName} | ||||||||||||||||||||||
| 135 | . ' name=' | ||||||||||||||||||||||
| 136 | . $a->{Name} . ' tag=' | ||||||||||||||||||||||
| 137 | . $a->{Tag} . '[' | ||||||||||||||||||||||
| 138 | . $a->{Code} . '] = ' | ||||||||||||||||||||||
| 139 | 0 | . $a->{RawValue} ); | |||||||||||||||||||||
| 140 | 0 | $attribute_result{ $a->{AttrName} } = $a->{RawValue}; | |||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||
| 142 | 0 | return \%attribute_result; | |||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||
| 144 | |||||||||||||||||||||||
| 145 | 0 | ▶ | sub check_pwd { | ||||||||||||||||||||
|
Subroutine
check_pwd: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||
| 146 | 0 | my ( $self, $req, $sessionInfo, $name, $pwd ) = @_; | |||||||||||||||||||||
| 147 | |||||||||||||||||||||||
| 148 | 0 | my @attributes; | |||||||||||||||||||||
| 149 | 0 | push @attributes, { Name => 1, Value => $name, Type => 'string' }; | |||||||||||||||||||||
| 150 | 0 | ▶ | if ($pwd) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 151 | 0 | push @attributes, { Name => 2, Value => $pwd, Type => 'string' }; | |||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||
| 153 | |||||||||||||||||||||||
| 154 | 0 | ▶ | while ( my ( $k, $r ) = each %{ $self->req_attributes_rules } ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 155 | 0 | my $v = eval { $r->( $req, $sessionInfo ) }; | |||||||||||||||||||||
| 156 | 0 | my $user = $req->user; | |||||||||||||||||||||
| 157 | 0 | ▶ | if ($@) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 158 | 0 | $self->logger->warn( "Error evaluating Radius attribute rule" | |||||||||||||||||||||
| 159 | . " for attribute $k of user $user: $@" ); | ||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||
| 161 | 0 | $self->logger->debug("Adding attribute $k => $v to Radius request"); | |||||||||||||||||||||
| 162 | 0 | push @attributes, { Name => $k, Value => $v, }; | |||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||
| 164 | |||||||||||||||||||||||
| 165 | 0 | return $self->_check_pwd_radius(@attributes); | |||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||
| 167 | |||||||||||||||||||||||
| 168 | 4 | 1; | |||||||||||||||||||||