Authen::Simple::NIS - Simple NIS authentication


Authen-Simple-NIS documentation Contained in the Authen-Simple-NIS distribution.

Index


Code Index:

NAME

Top

Authen::Simple::NIS - Simple NIS authentication

SYNOPSIS

Top

    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>

DESCRIPTION

Top

NIS authentication.

METHODS

Top

* new

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

* domain

NIS domain. Required unless it can be obtained from yp_get_default_domain().

    domain => 'domain'

* map

NIS map. Defaults to passwd.byname.

    map => 'passwd.byname'

* log

Any object that supports debug, info, error and warn.

    log => Log::Log4perl->get_logger('Authen::Simple::NIS')

* authenticate( $username, $password )

Returns true on success and false on failure.

SEE ALSO

Top

Authen::Simple.

Net::NIS.

Net::NIS::Table.

ypclnt(3).

AUTHOR

Top

Christian Hansen ch@ngmedia.com

COPYRIGHT

Top


Authen-Simple-NIS documentation Contained in the Authen-Simple-NIS distribution.

package Authen::Simple::NIS;

use strict;
use warnings;
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__