« 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.20'; # 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 | use Net::LDAP::SimpleServer::Constant; | ||
| 38 | |||
| 39 | my $BASEDIR = File::Spec->catfile( home(), '.ldapsimple' ); | ||
| 40 | my $DEFAULT_CONFIG_FILE = File::Spec->catfile( $BASEDIR, 'server.conf' ); | ||
| 41 | my $DEFAULT_DATA_FILE = File::Spec->catfile( $BASEDIR, 'server.ldif' ); | ||
| 42 | my $DEFAULT_LOG_FILE = File::Spec->catfile( $BASEDIR, 'server.log' ); | ||
| 43 | |||
| 44 | my @LDAP_PRIVATE_OPTIONS = qw/store input output/; | ||
| 45 | my @LDAP_PUBLIC_OPTIONS = | ||
| 46 | qw/data_file root_dn root_pw allow_anon user_passwords user_id_attr user_pw_attr user_filter/; | ||
| 47 | |||
| 48 | make_path($BASEDIR); | ||
| 49 | |||
| 50 | sub options { | ||
| 51 | my ( $self, $template ) = @_; | ||
| 52 | my $prop = $self->{server}; | ||
| 53 | |||
| 54 | ### setup options in the parent classes | ||
| 55 | $self->SUPER::options($template); | ||
| 56 | |||
| 57 | ### add a single value option | ||
| 58 | for (@LDAP_PUBLIC_OPTIONS) { | ||
| 59 | $prop->{$_} = undef unless exists $prop->{$_}; | ||
| 60 | $template->{$_} = \$prop->{$_}; | ||
| 61 | } | ||
| 62 | |||
| 63 | #use Data::Dumper; | ||
| 64 | #print STDERR Data::Dumper->Dump( [$self->{server}], ['server'] ); | ||
| 65 | return; | ||
| 66 | } | ||
| 67 | |||
| 68 | sub default_values { | ||
| 69 | my $self = @_; | ||
| 70 | |||
| 71 | my $v = {}; | ||
| 72 | $v->{port} = 389; | ||
| 73 | $v->{log_file} = $DEFAULT_LOG_FILE; | ||
| 74 | $v->{conf_file} = $DEFAULT_CONFIG_FILE if -r $DEFAULT_CONFIG_FILE; | ||
| 75 | $v->{syslog_ident} = | ||
| 76 | 'Net::LDAP::SimpleServer [' . $Net::LDAP::SimpleServer::VERSION . ']'; | ||
| 77 | |||
| 78 | $v->{allow_anon} = 1; | ||
| 79 | $v->{root_dn} = 'cn=root'; | ||
| 80 | $v->{data_file} = $DEFAULT_DATA_FILE if -r $DEFAULT_DATA_FILE; | ||
| 81 | $v->{user_passwords} = USER_PW_NONE; | ||
| 82 | $v->{user_filter} = '(objectClass=person)'; | ||
| 83 | $v->{user_id_attr} = 'uid'; | ||
| 84 | $v->{user_pw_attr} = 'userPassword'; | ||
| 85 | |||
| 86 | #use Data::Dumper; print STDERR Dumper($v); | ||
| 87 | return $v; | ||
| 88 | } | ||
| 89 | |||
| 90 | sub post_configure_hook { | ||
| 91 | my $self = shift; | ||
| 92 | my $prop = $self->{server}; | ||
| 93 | |||
| 94 | # create server directory in home dir | ||
| 95 | make_path($BASEDIR); | ||
| 96 | |||
| 97 | #use Data::Dumper; print STDERR '# ' . Dumper( $prop ); | ||
| 98 | croak q{Configuration has no "data_file" file!} | ||
| 99 | unless $prop->{data_file}; | ||
| 100 | croak qq{Cannot read data_file file (} . $prop->{data_file} . q{)} | ||
| 101 | unless -r $prop->{data_file}; | ||
| 102 | |||
| 103 | # data_file is not a "public" option in the server, it is created here | ||
| 104 | $prop->{store} = | ||
| 105 | Net::LDAP::SimpleServer::LDIFStore->new( $prop->{data_file} ) | ||
| 106 | || croak q{Cannot create data store!}; | ||
| 107 | |||
| 108 | return; | ||
| 109 | } | ||
| 110 | |||
| 111 | sub process_request { | ||
| 112 | my $self = shift; | ||
| 113 | my $prop = $self->{server}; | ||
| 114 | |||
| 115 | my $params = { map { ( $_ => $prop->{$_} ) } @LDAP_PUBLIC_OPTIONS }; | ||
| 116 | for (@LDAP_PRIVATE_OPTIONS) { | ||
| 117 | $params->{$_} = $prop->{$_} if $prop->{$_}; | ||
| 118 | } | ||
| 119 | $params->{sock} = $self->{server}->{client}; | ||
| 120 | my $handler = Net::LDAP::SimpleServer::ProtocolHandler->new($params); | ||
| 121 | |||
| 122 | until ( $handler->handle ) { | ||
| 123 | |||
| 124 | # intentionally empty loop | ||
| 125 | } | ||
| 126 | return; | ||
| 127 | } | ||
| 128 | |||
| 129 | 1; # Magic true value required at end of module | ||
| 130 | |||
| 131 | __END__ |