| 1 | # Apache::AppSamurai::AuthRadius - AppSamurai Radius authentication plugin | ||
| 2 | |||
| 3 | # $Id: AuthRadius.pm,v 1.15 2008/04/30 21:40:06 pauldoom Exp $ | ||
| 4 | |||
| 5 | ## | ||
| 6 | # Copyright (c) 2008 Paul M. Hirsch (paul@voltagenoir.org). | ||
| 7 | # All rights reserved. | ||
| 8 | # | ||
| 9 | # This program is free software; you can redistribute it and/or modify it under | ||
| 10 | # the same terms as Perl itself. | ||
| 11 | ## | ||
| 12 | |||
| 13 | package Apache::AppSamurai::AuthRadius; | ||
| 14 | use strict; | ||
| 15 | use warnings; | ||
| 16 | |||
| 17 | use vars qw($VERSION @ISA); | ||
| 18 | $VERSION = substr(q$Revision: 1.15 $, 10, -1); | ||
| 19 | |||
| 20 | use Carp; | ||
| 21 | use Apache::AppSamurai::AuthBase; | ||
| 22 | use Authen::Radius; | ||
| 23 | |||
| 24 | @ISA = qw( Apache::AppSamurai::AuthBase ); | ||
| 25 | |||
| 26 | sub Configure { | ||
| 27 | my $self = shift; | ||
| 28 | |||
| 29 | # Pull defaults from AuthBase and save. | ||
| 30 | $self->SUPER::Configure(); | ||
| 31 | my $conft = $self->{conf}; | ||
| 32 | |||
| 33 | # Initial configuration. Put defaults here before the @_ args are | ||
| 34 | # pulled in. | ||
| 35 | $self->{conf} = { %{$conft}, | ||
| 36 | Connect => '127.0.0.1:1812', # IP:port of RADIUS server | ||
| 37 | Secret => 'defaultisstupid', # RADIUS secret for this | ||
| 38 | # client | ||
| 39 | Timeout => 5, # Timeout for RADIUS auth to return | ||
| 40 | @_, | ||
| 41 | }; | ||
| 42 | return 1; | ||
| 43 | } | ||
| 44 | |||
| 45 | sub Initialize { | ||
| 46 | my $self = shift; | ||
| 47 | # Create our Authen::Radius instance | ||
| 48 | $self->{radius} = new Authen::Radius(Host => $self->{conf}{Connect}, | ||
| 49 | Secret => $self->{conf}{Secret}, | ||
| 50 | TimeOut => $self->{conf}{Timeout} | ||
| 51 | ); | ||
| 52 | ($self->{radius}) || ($self->AddError("Initialization of Authen::Radius failed: $!") && return 0); | ||
| 53 | |||
| 54 | $self->{init} = 1; | ||
| 55 | return 1; | ||
| 56 | } | ||
| 57 | |||
| 58 | |||
| 59 | # Query the Radius server | ||
| 60 | sub Authenticator { | ||
| 61 | my $self = shift; | ||
| 62 | my $user = shift; | ||
| 63 | my $pass = shift; | ||
| 64 | my $error; | ||
| 65 | |||
| 66 | # Amazingly enough, this actually sends the authentication request to the | ||
| 67 | # RADIUS server. Bet you couldn't figure THAT one out. | ||
| 68 | ($self->{radius}->check_pwd($user, $pass)) && (return 1); | ||
| 69 | |||
| 70 | # Save an error message if there is one, else assume a normal login failure | ||
| 71 | $error = $self->{radius}->get_error(); | ||
| 72 | if ($error ne 'ENONE') { | ||
| 73 | $self->AddError('error', "Special authentication failure: \"$user\": $error, " . $self->{radius}->strerror()); | ||
| 74 | } else { | ||
| 75 | $self->AddError('warn', "Authentication failure: \"$user\": " . $self->{radius}->strerror()); | ||
| 76 | } | ||
| 77 | |||
| 78 | # DEFAULT DENY # | ||
| 79 | return 0; | ||
| 80 | } | ||
| 81 | |||
| 82 | 1; # End of Apache::AppSamurai::AuthRadius | ||
| 83 | |||
| 84 | __END__ |