« Session/blib/lib/CGI/Session/Authenticator/LDAP.pm
↑ Summary
Session/blib/lib/CGI/Session/CGI.pm »
| 1 | #!/usr/local/bin/perl5 | ||
| 2 | # | ||
| 3 | |||
| 4 | package CGI::Session::Authorizor::NetLDAP; | ||
| 5 | |||
| 6 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 0 | ▶ | use Net::LDAP; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 8 | |||
| 9 | my %_params = ( -host => '-host', | ||
| 10 | -port => '-port', | ||
| 11 | -bind => '-bind', | ||
| 12 | -user => '-user', | ||
| 13 | -userdn => '-userdn', | ||
| 14 | -group => '-group', | ||
| 15 | -groupdn => '-groupdn', | ||
| 16 | -cafile => '-cafile', | ||
| 17 | -capath => '-capath', | ||
| 18 | -ciphers => '-ciphers', ); | ||
| 19 | |||
| 20 | sub _param | ||
| 21 | { | ||
| 22 | my $self = shift; | ||
| 23 | if ( scalar @_ == 1 ) | ||
| 24 | { | ||
| 25 | my $field = shift; | ||
| 26 | |||
| 27 | # | ||
| 28 | my $slot = $_params{$field}; | ||
| 29 | croak "Programmer Error: $field is not a known parameter" unless defined $slot; | ||
| 30 | return $self->{$slot}; | ||
| 31 | } | ||
| 32 | else | ||
| 33 | { | ||
| 34 | while( my $field = shift ) | ||
| 35 | { | ||
| 36 | my $slot = $_params{$field}; | ||
| 37 | |||
| 38 | # | ||
| 39 | croak "Programmer Error: $field is not a known parameter" unless defined $slot; | ||
| 40 | $self->{$slot} = shift; | ||
| 41 | } | ||
| 42 | } | ||
| 43 | } | ||
| 44 | |||
| 45 | sub set { _param(shift,@_); } | ||
| 46 | |||
| 47 | sub new | ||
| 48 | { | ||
| 49 | my $type = shift; | ||
| 50 | my $self = {}; | ||
| 51 | bless $self, $type; | ||
| 52 | $self->set( @_ ); | ||
| 53 | return $self; | ||
| 54 | } | ||
| 55 | |||
| 56 | sub host { set( shift(), '-host', @_ ); } | ||
| 57 | sub port { set( shift(), '-port', @_ ); } | ||
| 58 | sub groupdn { set( shift(), '-groupdn', @_ ); } | ||
| 59 | sub userdn { set( shift(), '-userdn', @_ ); } | ||
| 60 | sub cafile { set( shift(), '-cafile', @_ ); } | ||
| 61 | sub capath { set( shift(), '-capath', @_ ); } | ||
| 62 | sub ciphers { set( shift(), '-ciphers', @_ ); } | ||
| 63 | |||
| 64 | sub authorized | ||
| 65 | { | ||
| 66 | my ( $self, $auth_token ) = @_; | ||
| 67 | my $password = $auth_token->{-password}; | ||
| 68 | my $username = $auth_token->{-username}; | ||
| 69 | my $group = $auth_token->{-group}; | ||
| 70 | $username = defined $username ? $username : ""; | ||
| 71 | $group = defined $group ? $group : ""; | ||
| 72 | my %ld = Mozilla::LDAP::Utils::ldapArgs(); | ||
| 73 | my $host = $self->host(); | ||
| 74 | my $port = $self->port(); | ||
| 75 | my $userdn = $self->userdn(); | ||
| 76 | $userdn =~ s/\$username/$username/g; | ||
| 77 | |||
| 78 | # Make groups to search. | ||
| 79 | # | ||
| 80 | my @startdns; | ||
| 81 | if ( $auth_token->{-groupdn} ) | ||
| 82 | { | ||
| 83 | @startdns = @{ $auth_token->{$groupdn} }; | ||
| 84 | } | ||
| 85 | else | ||
| 86 | { | ||
| 87 | @startdns = @{ $self->{$groupdn} }; | ||
| 88 | foreach my $group ( @{$self->{$groupdn}} ) | ||
| 89 | { | ||
| 90 | @startdns = map { s/\$username/$username/g; s/\$group/$group/g; } @startdns; | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | # Handle SSL if neccessary. | ||
| 95 | # | ||
| 96 | my $cafile = $self->cafile(); | ||
| 97 | my $capath = $self->capath(); | ||
| 98 | my $ciphers = $self->ciphers(); | ||
| 99 | if ( $cafile or $capath ) | ||
| 100 | { | ||
| 101 | my %tls_args = ( verify=>'require' ); | ||
| 102 | $tls_args{cafile} = $cafile if $cafile; | ||
| 103 | $tls_args{capath} = $capath if $capath; | ||
| 104 | $tls_args{ciphers} = $ciphers if $ciphers; | ||
| 105 | my $result = $connection->start_tls( %tls_args ); | ||
| 106 | } | ||
| 107 | |||
| 108 | my $connection = new Mozilla::LDAP::Conn( $host, $port ); | ||
| 109 | my $visited = {}; | ||
| 110 | foreach my $dn ( @startdns ) | ||
| 111 | { | ||
| 112 | return 1 if contained_in( $connection, $visited, $dn, lc($userdn) ); | ||
| 113 | } | ||
| 114 | $connection->unbind(); | ||
| 115 | |||
| 116 | return $contians; | ||
| 117 | } | ||
| 118 | |||
| 119 | |||
| 120 | sub contained_in | ||
| 121 | { | ||
| 122 | my ( $connection, $visited, $currentdn, $userdn ) = @_; | ||
| 123 | return 1 if $visited{$currentdn}; | ||
| 124 | $visited->{$group}++; | ||
| 125 | return 1 if $userdn eq lc($currentdn); | ||
| 126 | my $entries = $connection->search( base=>$currentdn, | ||
| 127 | filter=>'(objectclass=groupofuniquenames)', | ||
| 128 | scope=>'base', | ||
| 129 | attrs=>[ 'objectclass', 'dn', 'uniquemember' ] ); | ||
| 130 | return 0 if $entries->code != LDAP_SUCCESS; | ||
| 131 | my $entry = $entries->pop_entry; | ||
| 132 | return 0 unless $entry; | ||
| 133 | return 0 unless $entry->exists( 'uniquemember' ); | ||
| 134 | foreach my $member ( $entry->get_value( 'uniquemember' ) ) | ||
| 135 | { | ||
| 136 | return 1 if contained_in( $connection, $visited, $member, $userdn ); | ||
| 137 | } | ||
| 138 | return 0; | ||
| 139 | } | ||
| 140 | |||
| 141 | |||
| 142 | |||
| 143 | |||
| 144 | =item CGI::Session::Authorizor::NetLDAP | ||
| 145 | |||
| 146 | An LDAP authorization module using Net::LDAP; | ||
| 147 | |||
| 148 | $auth = new CGI::Session::Authorizor::NetLDAP( -host => $host, | ||
| 149 | -port => $port, | ||
| 150 | -groupdn => 'cn=$group,ou=Groups,dc=mycorp,dc=com' ); | ||
| 151 | |||
| 152 | if ( $auth->authorized( $auth_token ) ) { ...success... } | ||
| 153 | else { ...failure... } | ||
| 154 | |||
| 155 | =item CGI::Session::Authorizor::NetLDAP; | ||
| 156 | |||
| 157 | It accepts on argument which is a reference to a hash. This hash contains | ||
| 158 | variables which are used to attempt authentication. | ||
| 159 | |||
| 160 | The standard values which are used for comparison. The basic ones are | ||
| 161 | '-username' and '-password'. | ||
| 162 | |||
| 163 | It returns a true value if the user was successfully authenticated, and a | ||
| 164 | false value if the user was not successfully authenticated. | ||
| 165 | |||
| 166 | =cut | ||
| 167 | |||
| 168 | |||
| 169 | 1; | ||