« blib/lib/Net/LDAP/SimpleServer/LDIFStore.pm
↑ Summary
lib/Net/LDAP/SimpleServer/ProtocolHandler.pm »
| 1 | package Net::LDAP::SimpleServer; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | # ABSTRACT: Minimal-configuration, read-only LDAP server | ||
| 7 | |||
| 8 | our $VERSION = '0.0.19'; # VERSION | ||
| 9 | |||
| 10 | use 5.008; | ||
| 11 | use Carp; | ||
| 12 | |||
| 13 | our $personality = undef; | ||
| 14 | |||
| 15 | sub import { | ||
| 16 | my $pkg = shift; | ||
| 17 | $personality = shift || 'Fork'; | ||
| 18 | |||
| 19 | eval "use base qw{Net::Server::$personality}"; ## no critic | ||
| 20 | croak $@ if $@; | ||
| 21 | |||
| 22 | push @Net::LDAP::SimpleServer::ISA, qw(Net::Server); | ||
| 23 | |||
| 24 | #use Data::Dumper; | ||
| 25 | #print STDERR Data::Dumper->Dump( [ \@Net::LDAP::SimpleServer::ISA ], | ||
| 26 | # ['ISA'] ); | ||
| 27 | return; | ||
| 28 | } | ||
| 29 | |||
| 30 | use File::Basename; | ||
| 31 | use File::HomeDir; | ||
| 32 | use File::Spec; | ||
| 33 | use File::Path 2.08 qw{make_path}; | ||
| 34 | use Scalar::Util qw{reftype}; | ||
| 35 | use Net::LDAP::SimpleServer::LDIFStore; | ||
| 36 | use Net::LDAP::SimpleServer::ProtocolHandler; | ||
| 37 | |||
| 38 | my $BASEDIR = File::Spec->catfile( home(), '.ldapsimple' ); | ||
| 39 | my $DEFAULT_CONFIG_FILE = File::Spec->catfile( $BASEDIR, 'server.conf' ); | ||
| 40 | my $DEFAULT_DATA_FILE = File::Spec->catfile( $BASEDIR, 'server.ldif' ); | ||
| 41 | my $DEFAULT_LOG_FILE = File::Spec->catfile( $BASEDIR, 'server.log' ); | ||
| 42 | |||
| 43 | my @LDAP_PRIVATE_OPTIONS = ( 'store', 'input', 'output' ); | ||
| 44 | my @LDAP_PUBLIC_OPTIONS = ( 'data_file', 'root_dn', 'root_pw', 'allow_anon' ); | ||
| 45 | |||
| 46 | make_path($BASEDIR); | ||
| 47 | |||
| 48 | sub options { | ||
| 49 | my ( $self, $template ) = @_; | ||
| 50 | my $prop = $self->{server}; | ||
| 51 | |||
| 52 | ### setup options in the parent classes | ||
| 53 | $self->SUPER::options($template); | ||
| 54 | |||
| 55 | ### add a single value option | ||
| 56 | for (@LDAP_PUBLIC_OPTIONS) { | ||
| 57 | $prop->{$_} = undef unless exists $prop->{$_}; | ||
| 58 | $template->{$_} = \$prop->{$_}; | ||
| 59 | } | ||
| 60 | |||
| 61 | #use Data::Dumper; | ||
| 62 | #print STDERR Data::Dumper->Dump( [$self->{server}], ['server'] ); | ||
| 63 | return; | ||
| 64 | } | ||
| 65 | |||
| 66 | sub default_values { | ||
| 67 | my $self = @_; | ||
| 68 | |||
| 69 | my $v = {}; | ||
| 70 | $v->{port} = 389; | ||
| 71 | $v->{log_file} = $DEFAULT_LOG_FILE; | ||
| 72 | $v->{conf_file} = $DEFAULT_CONFIG_FILE if -r $DEFAULT_CONFIG_FILE; | ||
| 73 | $v->{syslog_ident} = | ||
| 74 | 'Net::LDAP::SimpleServer [' . $Net::LDAP::SimpleServer::VERSION . ']'; | ||
| 75 | |||
| 76 | $v->{allow_anon} = 1; | ||
| 77 | $v->{root_dn} = 'cn=root'; | ||
| 78 | $v->{data_file} = $DEFAULT_DATA_FILE if -r $DEFAULT_DATA_FILE; | ||
| 79 | |||
| 80 | #use Data::Dumper; print STDERR Dumper($v); | ||
| 81 | return $v; | ||
| 82 | } | ||
| 83 | |||
| 84 | sub post_configure_hook { | ||
| 85 | my $self = shift; | ||
| 86 | my $prop = $self->{server}; | ||
| 87 | |||
| 88 | # create server directory in home dir | ||
| 89 | make_path($BASEDIR); | ||
| 90 | |||
| 91 | #use Data::Dumper; print STDERR '# ' . Dumper( $prop ); | ||
| 92 | croak q{Configuration has no "data_file" file!} | ||
| 93 | unless $prop->{data_file}; | ||
| 94 | croak qq{Cannot read data_file file (} . $prop->{data_file} . q{)} | ||
| 95 | unless -r $prop->{data_file}; | ||
| 96 | |||
| 97 | # data_file is not a "public" option in the server, it is created here | ||
| 98 | $prop->{store} = | ||
| 99 | Net::LDAP::SimpleServer::LDIFStore->new( $prop->{data_file} ) | ||
| 100 | || croak q{Cannot create data store!}; | ||
| 101 | |||
| 102 | return; | ||
| 103 | } | ||
| 104 | |||
| 105 | sub process_request { | ||
| 106 | my $self = shift; | ||
| 107 | my $prop = $self->{server}; | ||
| 108 | |||
| 109 | my $params = { map { ( $_ => $prop->{$_} ) } @LDAP_PUBLIC_OPTIONS }; | ||
| 110 | for (@LDAP_PRIVATE_OPTIONS) { | ||
| 111 | $params->{$_} = $prop->{$_} if $prop->{$_}; | ||
| 112 | } | ||
| 113 | $params->{sock} = $self->{server}->{client}; | ||
| 114 | my $handler = Net::LDAP::SimpleServer::ProtocolHandler->new($params); | ||
| 115 | |||
| 116 | until ( $handler->handle ) { | ||
| 117 | |||
| 118 | # intentionally empty loop | ||
| 119 | } | ||
| 120 | return; | ||
| 121 | } | ||
| 122 | |||
| 123 | 1; # Magic true value required at end of module | ||
| 124 | |||
| 125 | __END__ |