File Coverage

blib/lib/Authen/Simple/PAM.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package Authen::Simple::PAM;
2              
3 1     1   711 use strict;
  1         1  
  1         34  
4 1     1   5 use warnings;
  1         1  
  1         33  
5 1     1   15 use base 'Authen::Simple::Adapter';
  1         1  
  1         955  
6              
7 1     1   42727 use Authen::PAM qw[:constants];
  0            
  0            
8             use Params::Validate qw[];
9              
10             our $VERSION = 0.2;
11              
12             __PACKAGE__->options({
13             service => {
14             type => Params::Validate::SCALAR,
15             default => 'login',
16             optional => 1
17             }
18             });
19              
20             sub check {
21             my ( $self, $username, $password ) = @_;
22              
23             my $service = $self->service;
24             my $handler = sub {
25             my @response = ();
26              
27             while (@_) {
28             my $code = shift;
29             my $message = shift;
30             my $answer = undef;
31              
32             if ( $code == PAM_PROMPT_ECHO_ON ) {
33             $answer = $username;
34             }
35              
36             if ( $code == PAM_PROMPT_ECHO_OFF ) {
37             $answer = $password;
38             }
39              
40             push( @response, PAM_SUCCESS, $answer );
41             }
42              
43             return ( @response, PAM_SUCCESS );
44             };
45              
46              
47             my $pam = Authen::PAM->new( $service, $username, $handler );
48              
49             unless ( ref $pam ) {
50              
51             my $error = Authen::PAM->pam_strerror($pam);
52              
53             $self->log->error( qq/Failed to authenticate user '$username' using service '$service'. Reason: '$error'/ )
54             if $self->log;
55              
56             return 0;
57             }
58              
59             my $result = $pam->pam_authenticate;
60              
61             unless ( $result == PAM_SUCCESS ) {
62              
63             my $error = $pam->pam_strerror($result);
64              
65             $self->log->debug( qq/Failed to authenticate user '$username' using service '$service'. Reason: '$error'/ )
66             if $self->log;
67              
68             return 0;
69             }
70              
71             $result = $pam->pam_acct_mgmt;
72              
73             unless ( $result == PAM_SUCCESS ) {
74              
75             my $error = $pam->pam_strerror($result);
76              
77             $self->log->debug( qq/Failed to authenticate user '$username' using service '$service'. Reason: '$error'/ )
78             if $self->log;
79              
80             return 0;
81             }
82              
83             $self->log->debug( qq/Successfully authenticated user '$username' using service '$service'./ )
84             if $self->log;
85              
86             return 1;
87             }
88              
89             1;
90              
91             __END__