| 1 |
package Test::Debconf::DbDriver::SLAPD; |
| 2 |
use warnings; |
| 3 |
use strict; |
| 4 |
|
| 5 |
use Debconf::Gettext; |
| 6 |
|
| 7 |
use fields qw(server port dir conf ldif pidfile); |
| 8 |
|
| 9 |
sub new { |
| 10 |
my Test::Debconf::DbDriver::SLAPD $self = shift; |
| 11 |
unless (ref $self) { |
| 12 |
$self = fields::new($self); |
| 13 |
} |
| 14 |
$self->{server} = shift; |
| 15 |
$self->{port} = shift; |
| 16 |
my $base_dir = shift; |
| 17 |
$self->{dir} = "$base_dir/slapd"; |
| 18 |
|
| 19 |
$self->{conf} = "$self->{dir}/slapd.conf"; |
| 20 |
$self->{ldif} = "$self->{dir}/ldap.ldif"; |
| 21 |
$self->{pidfile} = "/tmp/slapd.pid"; |
| 22 |
|
| 23 |
return $self; |
| 24 |
} |
| 25 |
|
| 26 |
sub slapd_start{ |
| 27 |
my $self = shift; |
| 28 |
|
| 29 |
|
| 30 |
# print "beg slapd_start\n"; |
| 31 |
# be sure that we have no residues before starting new test |
| 32 |
$self->slapd_stop(); |
| 33 |
|
| 34 |
system("mkdir -p $self->{dir}") == 0 |
| 35 |
or die "Can not create tmp slapd data directory"; |
| 36 |
|
| 37 |
$self->build_slapd_conf(); |
| 38 |
$self->build_ldap_ldif(); |
| 39 |
|
| 40 |
# |
| 41 |
# start local slapd daemon for testing |
| 42 |
# |
| 43 |
my $slapdbin = '/usr/sbin/slapd'; |
| 44 |
my $slapaddbin = '/usr/sbin/slapadd'; |
| 45 |
|
| 46 |
# is there slapd installed? |
| 47 |
if (! -x $slapdbin) { |
| 48 |
die "Unable to find $slapdbin, is slapd package installed ?"; |
| 49 |
} |
| 50 |
|
| 51 |
system("$slapdbin -s LOG_DEBUG -f $self->{conf} -h ldap://$self->{server}:$self->{port}") == 0 |
| 52 |
or die "Error in slapd call"; |
| 53 |
system("$slapaddbin -f $self->{conf} -l $self->{ldif}") == 0 |
| 54 |
or die "Error in slapadd call"; |
| 55 |
|
| 56 |
# print "end slapd_start\n"; |
| 57 |
} |
| 58 |
|
| 59 |
# kill slapd daemon and delete sldap data files |
| 60 |
sub slapd_stop { |
| 61 |
my $self = shift; |
| 62 |
my $dir = $self->{dir}; |
| 63 |
my $pf = "/tmp/slapd.pid"; |
| 64 |
|
| 65 |
# print "beg slapd_stop\n"; |
| 66 |
|
| 67 |
if ( -f $pf) { |
| 68 |
# print $pf; |
| 69 |
open(my $pidfile, "<", $self->{pidfile}) or die "Can not open file: $pf"; |
| 70 |
my $pid = <$pidfile>; |
| 71 |
close $pidfile; |
| 72 |
my $cnt = kill 'TERM',$pid; |
| 73 |
sleep 1; |
| 74 |
# print $cnt; |
| 75 |
# system("rm $pf") == 0 |
| 76 |
# or die "Can not delete file: $pf"; |
| 77 |
} |
| 78 |
if ( -f $self->{conf}) { |
| 79 |
system("rm $self->{conf}") == 0 |
| 80 |
or die "Can not delete file: $self->{conf}"; |
| 81 |
} |
| 82 |
if ( -f $self->{ldif}) { |
| 83 |
system("rm $self->{ldif}") == 0 |
| 84 |
or die "Can not delete file: $self->{ldif}"; |
| 85 |
} |
| 86 |
system("rm -f $self->{dir}/*.dbb") == 0 |
| 87 |
or die "Can not delete .dbb files"; |
| 88 |
system("rm -rf $self->{dir}") == 0 |
| 89 |
or die "Can not delete .dbb files"; |
| 90 |
|
| 91 |
# print "end slapd_stop\n"; |
| 92 |
} |
| 93 |
|
| 94 |
sub build_slapd_conf { |
| 95 |
my $self = shift; |
| 96 |
|
| 97 |
open(my $slapd_conf, ">", "$self->{dir}/slapd.conf"); |
| 98 |
print $slapd_conf gettext(<<EOF); |
| 99 |
# This is the main ldapd configuration file. See slapd.conf(5) for more |
| 100 |
# info on the configuration options. |
| 101 |
|
| 102 |
modulepath /usr/lib/ldap |
| 103 |
moduleload back_ldbm |
| 104 |
|
| 105 |
# Schema and objectClass definitions |
| 106 |
include /etc/ldap/schema/core.schema |
| 107 |
include doc/debconf.schema |
| 108 |
|
| 109 |
# Schema check allows for forcing entries to |
| 110 |
# match schemas for their objectClasses's |
| 111 |
schemacheck on |
| 112 |
|
| 113 |
# Where the pid file is put. The init.d script |
| 114 |
# will not stop the server if you change this. |
| 115 |
pidfile $self->{pidfile} |
| 116 |
|
| 117 |
# List of arguments that were passed to the server |
| 118 |
argsfile $self-{dir}/slapd.args |
| 119 |
|
| 120 |
# Where to store the replica logs |
| 121 |
replogfile $self->{dir}/replog |
| 122 |
|
| 123 |
# Read slapd.conf(5) for possible values |
| 124 |
loglevel 0 |
| 125 |
|
| 126 |
####################################################################### |
| 127 |
# ldbm database definitions |
| 128 |
####################################################################### |
| 129 |
|
| 130 |
# The backend type, ldbm, is the default standard |
| 131 |
database ldbm |
| 132 |
|
| 133 |
# The base of your directory |
| 134 |
suffix "dc=debian,dc=org" |
| 135 |
|
| 136 |
# Where the database file are physically stored |
| 137 |
directory "$self->{dir}" |
| 138 |
|
| 139 |
# Indexing options |
| 140 |
index objectClass eq |
| 141 |
|
| 142 |
# Save the time that the entry gets modified |
| 143 |
lastmod on |
| 144 |
|
| 145 |
# The admin dn has full write access |
| 146 |
access to * |
| 147 |
by dn="cn=admin,dc=debian,dc=org" write |
| 148 |
by * read |
| 149 |
|
| 150 |
EOF |
| 151 |
|
| 152 |
close $slapd_conf; |
| 153 |
} |
| 154 |
|
| 155 |
sub build_ldap_ldif { |
| 156 |
my $self = shift; |
| 157 |
|
| 158 |
open(my $outfile, ">", "$self->{dir}/ldap.ldif"); |
| 159 |
print $outfile gettext(<<EOF); |
| 160 |
dn: cn=admin,dc=debian,dc=org |
| 161 |
objectClass: organizationalRole |
| 162 |
objectClass: simpleSecurityObject |
| 163 |
cn: admin |
| 164 |
description: LDAP administrator |
| 165 |
userPassword: debian |
| 166 |
|
| 167 |
dn: cn=debconf,dc=debian,dc=org |
| 168 |
objectClass: applicationProcess |
| 169 |
cn: debconf |
| 170 |
|
| 171 |
EOF |
| 172 |
|
| 173 |
close $outfile; |
| 174 |
} |
| 175 |
|
| 176 |
1; |