Authen-Simple-NIS

 view release on metacpan or  search on metacpan

lib/Authen/Simple/NIS.pm  view on Meta::CPAN

use base 'Authen::Simple::Adapter';

use Net::NIS         qw[YPERR_KEY YPERR_SUCCESS];
use Net::NIS::Table  qw[];
use Params::Validate qw[];

our $VERSION = 0.3;

__PACKAGE__->options({
    domain => {
        type     => Params::Validate::SCALAR,
        optional => 1
    },
    map => {
        type     => Params::Validate::SCALAR,
        default  => 'passwd.byname',
        optional => 1
    }
});

sub check {
    my ( $self, $username, $password ) = @_;

    my $domain = $self->domain;

    unless ( $domain ||= Net::NIS::yp_get_default_domain() ) {

        $self->log->error( qq/Failed to obtain default NIS domain./ )
          if $self->log;

        return 0;
    }

    my $nis   = Net::NIS::Table->new( $self->map, $domain );
    my $entry = $nis->match($username);

    unless ( $nis->status == YPERR_SUCCESS ) {

        my $map = $self->map;

        if ( $nis->status == YPERR_KEY ) {

            $self->log->debug( qq/User '$username' was not found in map '$map' from domain '$domain'./ )
              if $self->log;
        }
        else {

            my $error = Net::NIS::yperr_string( $nis->status );

            $self->log->error( qq/Failed to lookup key '$username' in map '$map' from domain '$domain'. Reason: '$error'/ )
              if $self->log;
        }

        return 0;
    }

    my $encrypted = ( split( /:/, $entry ) )[1];

    unless ( $self->check_password( $password, $encrypted ) ) {

        $self->log->debug( qq/Failed to authenticate user '$username'. Reason: 'Invalid credentials'/ )
          if $self->log;

        return 0;
    }

    $self->log->debug( qq/Successfully authenticated user '$username' using domain '$domain'./ )
      if $self->log;

    return 1;
}

1;

__END__

=head1 NAME

Authen::Simple::NIS - Simple NIS authentication

=head1 SYNOPSIS

    use Authen::Simple::NIS;
    
    my $nis = Authen::Simple::NIS->new;
    
    if ( $nis->authenticate( $username, $password ) ) {
        # successfull authentication
    }
    
    # or as a mod_perl Authen handler

    PerlModule Authen::Simple::Apache
    PerlModule Authen::Simple::NIS

    PerlSetVar AuthenSimpleNIS_domain "domain"

    <Location /protected>
      PerlAuthenHandler Authen::Simple::NIS
      AuthType          Basic
      AuthName          "Protected Area"
      Require           valid-user
    </Location>

=head1 DESCRIPTION

NIS authentication.

=head1 METHODS

=over 4

=item * new

This method takes a hash of parameters. The following options are
valid:

=over 8

=item * domain



( run in 2.392 seconds using v1.01-cache-2.11-cpan-cdf2f3d4e48 )