Modules with several packages
am 16.10.2005 10:50:53 von larsHi list,
I'm trying to develop a mod_perl module with several packages.
At first, I created to separate modules called 'Skjaerlund::Apache' and
'Skjaerlund::Apache::Authenticate'. The first module is mandatory and
includes all the definitions for the Apache configuration file, whereas the
second module authenticates users against Novells eDir database through
LDAP.
In our world, that means Net::LDAP, of course. When done as described
above,
everything works fine, but whenever I move the code of the second module
into the first module, things start acting weird - or maybe it's just my
lack of understanding ;-).
Anyway: Everything works fine when I have the two packages in two separate
files. When I run them from one module file, Net::LDAP works exactly four
times. I know because I log a line of the LDAP return code everytime I call
the authentication routine. The fifth time Net::LDAP is called, it returns
'Operations error' - which usually means some syntax error, but the code is
exactly the same as in the preceding four calls - and the sixth time it
returns "Can't contact LDAP server" and Apache dies.
Can anyone shed some light on this mystery?
The code goes here:
package Skjaerlund::Apache;
use strict;
use warnings;
require Exporter;
use Apache::Constants qw(:common);
use Apache::ModuleConfig ();
use DynaLoader ();
use Net::LDAP;
our @ISA = qw(DynaLoader Exporter);
our %EXPORT_TAGS = ( 'all' => [ qw(
) ] );
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
our @EXPORT = qw(handler);
our $VERSION = '1.00';
if ($ENV{MOD_PERL}) {
__PACKAGE__->bootstrap($VERSION);
}
sub new {
return bless {}, shift;
}
sub handler {
return OK;
}
sub NDS_Context ($$$) {
my ($cfg, $parms, $arg) = @_;
my $scfg = Apache::ModuleConfig->get($parms->server);
$scfg->{NDSContext} = $arg;
}
sub NDS_LDAPBindUser ($$$) {
my ($cfg, $parms, $arg) = @_;
my $scfg = Apache::ModuleConfig->get($parms->server);
$scfg->{NDS_LDAPBindUser} = $arg;
}
sub NDS_LDAPBindPassword ($$$) {
my ($cfg, $parms, $arg) = @_;
my $scfg = Apache::ModuleConfig->get($parms->server);
$scfg->{NDS_LDAPBindPassword} = $arg;
}
sub NDS_LDAPServer ($$$) {
my ($cfg, $parms, $arg) = @_;
my $scfg = Apache::ModuleConfig->get($parms->server);
$scfg->{NDS_LDAPServer} = $arg;
}
1;
package Skjaerlund::Apache::Authenticate;
use strict;
use Apache::Constants qw(:common);
use Net::LDAP;
my $ldap;
sub BEGIN {
$ldap = Net::LDAP->new('localhost', version => 3);
my $msg = $ldap->start_tls(verify => 'require', cafile =>
'/etc/ssl/certs/cacert.pem');
$msg->code && die $msg->error;
$msg = $ldap->bind('cn=admin,o=admin', password => '******');
$msg->code && die $msg->error;
}
sub handler {
my $r = shift;
my($result, $password) = $r->get_basic_auth_pw;
return $result if $result != OK;
my $user = $r->connection->user;
my $cfg = Apache::ModuleConfig->get($r->server, 'Skjaerlund::Apache');
$user = 'cn=' . $user . ',' . $cfg->{NDSContext};
my $msg = $ldap->compare($user, attr => 'userpassword', value =>
$password);
warn "LDAP returned ", $msg->error, " code ", $msg->code;
if ($msg->code == 6) {
return OK;
} else {
return AUTH_REQUIRED;
}
}
1;
Regards,
Lars
--
Lars Skjærlund, Network Consultant, Spinn International ApS
Bukkeballevej 30, 2960 Rungsted Kyst, Denmark
Tel.: +45 70 25 88 10, Fax: +45 70 25 88 44
Mail: lars@spinn.dk Web: http://www.spinn.dk