Skip to content

Commit

Permalink
Multiple Changes
Browse files Browse the repository at this point in the history
Small fixes
- fixed copyright date
- lost common sense
- some cleanup in useless comments
- added ldapd.pl script
- changed option name from 'ldap_data' to 'data_file'
- changed server basedir to {home}/.ldapsimple
- log file goes into {basedir}/server.log by default,
  with no additional directory level
- changed default configuration file to {basedir}/server.conf
- added option 'allow_anon' to control whether the server accepts anonymous
  binds or not

Improved tests
- added ldif file with multiple entries for testing
- changed server log path in all tests to /tmp/ldapserver.log
- renamed test files
- fixed t/03-param.t -> t/13-param.t
-- more tests
-- better test logic (functions server_ok() and server_nok())
-- added test names
- fixing t/04-bind.t -> t/14-bind.t
-- replace 'use constant' with variables
-- improved test messages
- test for unsupported authentication mechanism
- refactored common test code to t/lib/Helper.pm

SimpleServer.pm
- adding Net::Server to @isa, rather than assigning it
- using variables rather than constants
- only creates a store if one is not provided

ProtocolHandler
- improved invokation style for the constructor - now everything goes inside
  the hash reference
- explicitly exporting the symbols from Net::LDAP::Constant
- improved error mesages
- added option 'allow_anon' to control whether to accept anonymous binds

Tidy

Work still in progress ...

Signed-off-by: Alexei Znamensky <russoz@cpan.org>
  • Loading branch information
russoz committed Jul 24, 2012
1 parent c6aaacb commit 58f60bd
Show file tree
Hide file tree
Showing 17 changed files with 514 additions and 352 deletions.
32 changes: 32 additions & 0 deletions bin/ldapd.pl
@@ -0,0 +1,32 @@
#!/usr/bin/env perl

use strict;
use warnings;

# PODNAME: ldapd.pl
# ABSTRACT: Script to invoke the LDAP server.

# VERSION

use Net::LDAP::SimpleServer;

my $server =
@ARGV
? Net::LDAP::SimpleServer->new( {@ARGV} )
: Net::LDAP::SimpleServer->new;

$server->run();

__END__
=head1 SYNOPSIS
host:~ # ldapd.pl
=head1 DESCRIPTION
This script simply instantiates and executes a L<Net::LDAP::SimpleServer>
server.
=cut
2 changes: 1 addition & 1 deletion dist.ini
Expand Up @@ -2,7 +2,7 @@ name = Net-LDAP-SimpleServer
author = Alexei Znamensky <russoz@cpan.org>
license = Perl_5
copyright_holder = Alexei Znamensky
copyright_year = 2011
copyright_year = 2012

[@Author::RUSSOZ]
version = gitnext
Expand Down
4 changes: 0 additions & 4 deletions examples/empty.conf
@@ -1,5 +1,3 @@
#-------------- file test.conf --------------

### user and group to become
#user somebody
#group everybody
Expand Down Expand Up @@ -37,5 +35,3 @@
### reverse lookups ?
# reverse_lookups on

#-------------- file test.conf --------------

43 changes: 43 additions & 0 deletions examples/multi-entries.ldif
@@ -0,0 +1,43 @@
version: 1

dn: CN=John Doe,OU=Marketing,DC=Company,DC=com
objectClass: top
objectClass: person
objectClass: organizationalPerson
objectClass: user
cn: John Doe
description: Consultant - Company.com
displayName: Joe Doe
distinguishedName: CN=John Doe,OU=Marketing,DC=Company,DC=com
givenName: John
manager: CN=Jack Puppetmeister,OU=Marketing,DC=Company,DC=com
name: John Doe
sn: Doe

dn: CN=Sarah Lee,OU=Marketing,DC=Company,DC=com
objectClass: top
objectClass: person
objectClass: organizationalPerson
objectClass: user
cn: Sarah Lee
description: Consultant - Company.com
displayName: Sarah Lee
distinguishedName: CN=Sarah Lee,OU=Marketing,DC=Company,DC=com
givenName: Sarah
manager: CN=Jack Puppetmeister,OU=Marketing,DC=Company,DC=com
name: Sarah Lee
sn: Lee

dn: CN=Robert Sponge,OU=Marketing,DC=Company,DC=com
objectClass: top
objectClass: person
objectClass: organizationalPerson
objectClass: user
cn: Robert Sponge
description: Consultant - Company.com
displayName: Bob Sponge
distinguishedName: CN=Robert Sponge,OU=Marketing,DC=Company,DC=com
givenName: Robert
manager: CN=John Doe,OU=Marketing,DC=Company,DC=com
name: Robert Sponge
sn: Sponge
4 changes: 2 additions & 2 deletions examples/single-entry.conf
@@ -1,12 +1,12 @@

ldap_data examples/single-entry.ldif
data_file examples/single-entry.ldif

### user and group to become
#user somebody
#group everybody

### logging ?
#log_file /var/log/server.log
log_file /tmp/ldapserver.log
#log_level 3
#pid_file /tmp/server.pid

Expand Down
112 changes: 48 additions & 64 deletions lib/Net/LDAP/SimpleServer.pm
Expand Up @@ -9,19 +9,17 @@ use warnings;

use 5.008;
use Carp;
use common::sense;

our $personality = undef;

sub import {
my $pkg = shift;
$personality = shift || 'Fork';

use Net::Server;
eval "use base qw{Net::Server::$personality}"; ## no critic
croak $@ if $@;

@Net::LDAP::SimpleServer::ISA = qw(Net::Server);
push @Net::LDAP::SimpleServer::ISA, qw(Net::Server);

#use Data::Dumper;
#print STDERR Data::Dumper->Dump( [ \@Net::LDAP::SimpleServer::ISA ],
Expand All @@ -37,97 +35,85 @@ use Scalar::Util qw{reftype};
use Net::LDAP::SimpleServer::LDIFStore;
use Net::LDAP::SimpleServer::ProtocolHandler;

## no critic
use constant BASEDIR => File::Spec->catfile( home(), '.ldapsimpleserver' );
use constant LOGDIR => File::Spec->catfile( BASEDIR, 'log' );
use constant DEFAULT_CONFIG_FILE => File::Spec->catfile( BASEDIR, 'config' );
use constant DEFAULT_DATA_FILE => File::Spec->catfile( BASEDIR, 'server.ldif' );
## use critic
my $BASEDIR = File::Spec->catfile( home(), '.ldapsimple' );
my $DEFAULT_CONFIG_FILE = File::Spec->catfile( $BASEDIR, 'server.conf' );
my $DEFAULT_DATA_FILE = File::Spec->catfile( $BASEDIR, 'server.ldif' );

make_path(LOGDIR);
my @LDAP_PRIVATE_OPTIONS = ( 'store', 'input', 'output' );
my @LDAP_PUBLIC_OPTIONS = ( 'data_file', 'root_dn', 'root_pw', 'allow_anon' );

my $_add_option = sub {
my ( $template, $prop, $opt, $initial ) = @_;

$prop->{$opt} = $initial;
$template->{$opt} = \$prop->{$opt};
};
make_path($BASEDIR);

sub options {
my ( $self, $template ) = @_;
my $prop = $self->{server};

### setup options in the parent classes
$self->SUPER::options($template);

### add a single value option
my $prop = $self->{server};
$_add_option->( $template, $prop, 'ldap_data', undef );
$_add_option->( $template, $prop, 'root_dn', undef );
$_add_option->( $template, $prop, 'root_pw', undef );
for (@LDAP_PUBLIC_OPTIONS) {
$prop->{$_} = undef unless exists $prop->{$_};
$template->{$_} = \$prop->{$_};
}

#use Data::Dumper;
#print STDERR Data::Dumper->Dump( [$self], ['options_END'] );
#print STDERR Data::Dumper->Dump( [$self->{server}], ['server'] );
return;
}

sub default_values {
my $self = @_;

my $v = {};
$v->{port} = 389;
$v->{root_dn} = 'cn=root';
$v->{root_pw} = 'ldappw';
$v->{log_file} = File::Spec->catfile( LOGDIR, 'server.log' );

#$v->{pid_file} = File::Spec->catfile( LOGDIR, 'server.pid' );
$v->{conf_file} = DEFAULT_CONFIG_FILE if -r DEFAULT_CONFIG_FILE;
$v->{ldap_data} = DEFAULT_DATA_FILE if -r DEFAULT_DATA_FILE;
$v->{port} = 389;
$v->{log_file} = File::Spec->catfile( $BASEDIR, 'server.log' );
$v->{conf_file} = $DEFAULT_CONFIG_FILE if -r $DEFAULT_CONFIG_FILE;
$v->{syslog_ident} =
'Net::LDAP::SimpleServer-' . $Net::LDAP::SimpleServer::VERSION;
return $v;
}

sub _make_dir {
my $file = shift;
return unless $file;
'Net::LDAP::SimpleServer [' . $Net::LDAP::SimpleServer::VERSION . ']';

my $dir = dirname($file);
return unless $dir;
return if -d $dir;
$v->{allow_anon} = 1;
$v->{root_dn} = 'cn=root';
$v->{data_file} = $DEFAULT_DATA_FILE if -r $DEFAULT_DATA_FILE;

make_path($dir);
return;
#use Data::Dumper; print STDERR Dumper($v);
return $v;
}

sub post_configure_hook {
my $self = shift;
my $prop = $self->{server};

#use Data::Dumper;
#print STDERR '# ' . Data::Dumper->Dump( [$self], ['post_configure_hook'] );
croak q{Cannot find conf file "} . $self->{server}->{conf_file} . q{"}
if $self->{server}->{conf_file} and not -r $self->{server}->{conf_file};
_make_dir( $self->{server}->{log_file} );
_make_dir( $self->{server}->{pid_file} );
croak q{Configuration has no "ldap_data" file!}
unless exists $prop->{ldap_data};
croak qq{Cannot read ldap_data file "} . $prop->{ldap_data} . q{"}
unless -r $prop->{ldap_data};
# create server directory in home dir
make_path($BASEDIR);

#use Data::Dumper; print STDERR '# ' . Dumper( $prop );
croak q{Cannot read configuration file (} . $prop->{conf_file} . q{)}
if ( $prop->{conf_file} && !-r $prop->{conf_file} );
croak q{Configuration has no "data_file" file!}
unless $prop->{data_file};
croak qq{Cannot read data_file file (} . $prop->{data_file} . q{)}
unless -r $prop->{data_file};

# data_file is not a "public" option in the server, it is created here
$prop->{store} =
Net::LDAP::SimpleServer::LDIFStore->new( $prop->{ldap_data} )
Net::LDAP::SimpleServer::LDIFStore->new( $prop->{data_file} )
|| croak q{Cannot create data store!};

return;
}

sub process_request {
my $self = shift;
my $prop = $self->{server};

my $in = *STDIN{IO};
my $out = *STDOUT{IO};
my $params =
{ map { ( $_ => $self->{server}->{$_} ) } qw/store root_dn root_pw/ };
my $handler =
Net::LDAP::SimpleServer::ProtocolHandler->new( $params, $in, $out );
my $params = { map { ( $_ => $prop->{$_} ) } @LDAP_PUBLIC_OPTIONS };
for (@LDAP_PRIVATE_OPTIONS) {
$params->{$_} = $prop->{$_} if $prop->{$_};
}
$params->{input} = *STDIN{IO};
$params->{output} = *STDOUT{IO};
my $handler = Net::LDAP::SimpleServer::ProtocolHandler->new($params);

until ( $handler->handle ) {

Expand All @@ -142,8 +128,6 @@ __END__
=head1 SYNOPSIS
package MyServer;
use Net::LDAP::SimpleServer;
# Or, specifying a Net::Server personality
Expand All @@ -160,7 +144,7 @@ __END__
# passing configurations in a hash
my $server = Net::LDAP::SimpleServer->new({
port => 5000,
ldap_data => '/path/to/data.ldif',
data_file => '/path/to/data.ldif',
});
# make it spin
Expand Down Expand Up @@ -202,7 +186,7 @@ server, namely:
=over
ldap_data - the LDIF data file used by LDIFStore
data_file - the LDIF data file used by LDIFStore
root_dn - the administrator DN of the repository
Expand All @@ -224,7 +208,7 @@ number of options. In Net::LDAP::SimpleServer, this method is defined as:
root_pw => 'ldappw',
syslog_ident => 'Net::LDAP::SimpleServer-'
. $Net::LDAP::SimpleServer::VERSION,
conf_file => DEFAULT_CONFIG_FILE,
conf_file => $DEFAULT_CONFIG_FILE,
};
}
Expand All @@ -249,7 +233,7 @@ server settings. If no file is specified and options are not passed
in a hash, this module will look for a default configuration file named
C<< ${HOME}/.ldapsimpleserver/config >>.
ldap_data /path/to/a/ldif/file.ldif
data_file /path/to/a/ldif/file.ldif
#port 389
#root_dn cn=root
#root_pw somepassword
Expand Down
1 change: 0 additions & 1 deletion lib/Net/LDAP/SimpleServer/LDIFStore.pm
Expand Up @@ -2,7 +2,6 @@ package Net::LDAP::SimpleServer::LDIFStore;

use strict;
use warnings;
use diagnostics;

# ABSTRACT: Data store to support Net::LDAP::SimpleServer

Expand Down

0 comments on commit 58f60bd

Please sign in to comment.