From d06e927b66e2e33145d67744fc2818b4a3d5de10 Mon Sep 17 00:00:00 2001 From: Nigel Kukard <nkukard@lbsd.net> Date: Tue, 28 Jul 2009 11:22:20 +0000 Subject: [PATCH] * Added support for vendor attributes in the form of [vendor_id:attribute_name] --- smradius/attributes.pm | 42 ++++++++++++++++------ smradius/modules/userdb/mod_userdb_sql.pm | 11 ++++-- smradius/modules/userdb/mod_userdb_test.pm | 10 ++++-- smradiusd | 18 ++++++++-- 4 files changed, 63 insertions(+), 18 deletions(-) diff --git a/smradius/attributes.pm b/smradius/attributes.pm index b5ebb4e1..39725718 100644 --- a/smradius/attributes.pm +++ b/smradius/attributes.pm @@ -57,35 +57,57 @@ my @attributeVReplyIgnoreList = ( ); -## @fn addAttribute($server,$attributes,$attribute) +## @fn addAttribute($server,$nattributes,$vattributes,$attribute) # Function to add an attribute to $attributes # # @param server Server instance -# @param attributes Hashref of attributes we already have and / or must add to +# @param nattributes Hashref of normal attributes we already have and/or must add to +# @param vattributes Hashref of vendor attributes we already have and/or must add to # @param attribute Attribute to add, eg. Those from a database sub addAttribute { - my ($server,$attributes,$attribute) = @_; + my ($server,$nattributes,$vattributes,$attribute) = @_; + + # Clean them up a bit + $attribute->{'Name'} =~ s/\s*(\S+)\s*/$1/; + $attribute->{'Operator'} =~ s/\s*(\S+)\s*/$1/; + # Grab attribue name, operator and value + my $name = $attribute->{'Name'}; + my $operator = $attribute->{'Operator'}; + my $value = $attribute->{'Value'}; + # Default attribute to add is normal + my $attributes = $nattributes; + + # Check where we must add this attribute, maybe to the vendor attributes? + if ($name =~ /^\[(\d+):(\S+)\]$/) { + my $vendor = $1; $name = $2; + # Set vendor + $attribute->{'Vendor'} = $vendor; + # Reset attribute name + $attribute->{'Name'} = $name; + # Set the attributes to use to the vendor + $attributes = $vattributes; + } # Check if this is an array - if ($attribute->{'Operator'} =~ s/^\|\|//) { + if ($operator =~ s/^\|\|//) { # Check if we've seen this before - if (defined($attributes->{$attribute->{'Name'}}->{$attribute->{'Operator'}}) && - ref($attributes->{$attribute->{'Name'}}->{$attribute->{'Operator'}}->{'Value'}) eq "ARRAY" ) { + if (defined($attributes->{$name}->{$operator}) && + ref($attributes->{$name}->{$operator}->{'Value'}) eq "ARRAY" ) { # Then add value to end of array - push(@{$attributes->{$attribute->{'Name'}}->{$attribute->{'Operator'}}->{'Value'}}, $attribute->{'Value'}); + push(@{$attributes->{$name}->{$operator}->{'Value'}}, $value); # If we have not seen it before, initialize it } else { # Assign attribute - $attributes->{$attribute->{'Name'}}->{$attribute->{'Operator'}} = $attribute; + $attributes->{$name}->{$operator} = $attribute; # Override type ... else we must create a custom attribute hash, this is dirty, but faster - $attributes->{$attribute->{'Name'}}->{$attribute->{'Operator'}}->{'Value'} = [ $attribute->{'Value'} ]; + $attributes->{$name}->{$operator}->{'Value'} = [ $value ]; } # If its not an array, just add it normally } else { - $attributes->{$attribute->{'Name'}}->{$attribute->{'Operator'}} = $attribute; + $attributes->{$name}->{$operator} = $attribute; } } diff --git a/smradius/modules/userdb/mod_userdb_sql.pm b/smradius/modules/userdb/mod_userdb_sql.pm index 43629718..8d3b764c 100644 --- a/smradius/modules/userdb/mod_userdb_sql.pm +++ b/smradius/modules/userdb/mod_userdb_sql.pm @@ -202,6 +202,7 @@ sub get # Attributes to return my %attributes = (); + my %vattributes = (); # Replace template entries my @dbDoParams = templateReplace($config->{'userdb_get_group_attributes_query'},$template); @@ -215,7 +216,7 @@ sub get # Loop with group attributes while (my $row = $sth->fetchrow_hashref()) { - addAttribute($server,\%attributes,hashifyLCtoMC($row,qw(Name Operator Value))); + addAttribute($server,\%attributes,\%vattributes,hashifyLCtoMC($row,qw(Name Operator Value))); } DBFreeRes($sth); @@ -233,12 +234,16 @@ sub get # Loop with group attributes while (my $row = $sth->fetchrow_hashref()) { - addAttribute($server,\%attributes,hashifyLCtoMC($row,qw(Name Operator Value))); + addAttribute($server,\%attributes,\%vattributes,hashifyLCtoMC($row,qw(Name Operator Value))); } DBFreeRes($sth); - return \%attributes; + my $ret; + $ret->{'Attributes'} = \%attributes; + $ret->{'VAttributes'} = \%vattributes; + + return $ret; } diff --git a/smradius/modules/userdb/mod_userdb_test.pm b/smradius/modules/userdb/mod_userdb_test.pm index 217b328d..ff4e5092 100644 --- a/smradius/modules/userdb/mod_userdb_test.pm +++ b/smradius/modules/userdb/mod_userdb_test.pm @@ -91,8 +91,8 @@ sub get { my ($server,$user,$packet) = @_; - - my $userDetails = { + # Attributes to return + my $attributes = { 'ClearPassword' => 'doap', 'Attributes' => [ { @@ -113,7 +113,13 @@ sub get ] }; + my %vattributes = (); + + my $ret; + $ret->{'Attributes'} = $attributes; + $ret->{'VAttributes'} = \%vattributes; + return $ret; return $userDetails; } diff --git a/smradiusd b/smradiusd index 9f44cca8..f3c216a0 100755 --- a/smradiusd +++ b/smradiusd @@ -606,7 +606,8 @@ sub process_request { # Check result if (defined($res) && ref($res) eq "HASH") { # We're only after the attributes here - $user->{'Attributes'} = $res; + $user->{'Attributes'} = $res->{'Attributes'}; + $user->{'VAttributes'} = $res->{'VAttributes'}; } } @@ -755,7 +756,8 @@ sub process_request { goto CHECK_RESULT; } # Setup user dataw - $user->{'Attributes'} = $res; + $user->{'Attributes'} = $res->{'Attributes'}; + $user->{'VAttributes'} = $res->{'VAttributes'}; } else { $self->log(LOG_INFO,"[SMRADIUS] GET: No 'User_get' function available for module '".$userdb->{'Name'}."'"); @@ -885,9 +887,19 @@ sub process_request { $resp->set_attr($attrName,$value); } } - +use Data::Dumper; print Dumper($user->{'ReplyVAttributes'}); +use Data::Dumper; print Dumper($user->{'VAttributes'}); # Loop with vendor reply attributes my %replyVAttributes = %{ $user->{'ReplyVAttributes'} }; + foreach my $attrName (keys %{$user->{'VAttributes'}}) { + # Loop with operators + foreach my $attrOp (keys %{$user->{'VAttributes'}->{$attrName}}) { + # Grab attribute + my $attr = $user->{'VAttributes'}->{$attrName}->{$attrOp}; + # Add this to the reply attribute? + setReplyVAttribute($self,\%replyVAttributes,$attr); + } + } foreach my $vendor (keys %replyVAttributes) { # Loop with operators foreach my $attrName (keys %{$replyVAttributes{$vendor}}) { -- GitLab