summaryrefslogtreecommitdiffstats
path: root/src/test/ldap/LdapServer.pm
blob: 2ff580e5d468dcc3a38cc5cbeae78cc2938a227a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
############################################################################
#
# LdapServer.pm
#
# Module to set up an LDAP server for testing pg_hba.conf ldap authentication
#
# Copyright (c) 2023, PostgreSQL Global Development Group
#
############################################################################

=pod

=head1 NAME

LdapServer - class for an LDAP server for testing pg_hba.conf authentication

=head1 SYNOPSIS

  use LdapServer;

  # have we found openldap binaries suitable for setting up a server?
  my $ldap_binaries_found = $LdapServer::setup;

  # create a server with the given root password and auth type
  # (users or anonymous)
  my $server = LdapServer->new($root_password, $auth_type);

  # Add the contents of an LDIF file to the server
  $server->ldapadd_file ($path_to_ldif_data);

  # set the Ldap password for a user
  $server->ldapsetpw($user, $password);

  # get details of some settings for the server
  my @properties = $server->prop($propname1, $propname2, ...);

=head1 DESCRIPTION

  LdapServer tests in its INIT phase for the presence of suitable openldap
  binaries. Its constructor method sets up and runs an LDAP server, and any
  servers that are set up are terminated during its END phase.

=cut

package LdapServer;

use strict;
use warnings;

use PostgreSQL::Test::Utils;
use Test::More;

use File::Copy;
use File::Basename;

# private variables
my ($slapd, $ldap_schema_dir, @servers);

# visible variables
our ($setup, $setup_error);

INIT
{
	# Find the OpenLDAP server binary and directory containing schema
	# definition files.  On success, $setup is set to 1. On failure,
	# it's set to 0, and an error message is set in $setup_error.
	$setup = 1;
	if ($^O eq 'darwin')
	{
		if (-d '/opt/homebrew/opt/openldap')
		{
			# typical paths for Homebrew on ARM
			$slapd = '/opt/homebrew/opt/openldap/libexec/slapd';
			$ldap_schema_dir = '/opt/homebrew/etc/openldap/schema';
		}
		elsif (-d '/usr/local/opt/openldap')
		{
			# typical paths for Homebrew on Intel
			$slapd = '/usr/local/opt/openldap/libexec/slapd';
			$ldap_schema_dir = '/usr/local/etc/openldap/schema';
		}
		elsif (-d '/opt/local/etc/openldap')
		{
			# typical paths for MacPorts
			$slapd = '/opt/local/libexec/slapd';
			$ldap_schema_dir = '/opt/local/etc/openldap/schema';
		}
		else
		{
			$setup_error = "OpenLDAP server installation not found";
			$setup = 0;
		}
	}
	elsif ($^O eq 'linux')
	{
		if (-d '/etc/ldap/schema')
		{
			$slapd = '/usr/sbin/slapd';
			$ldap_schema_dir = '/etc/ldap/schema';
		}
		elsif (-d '/etc/openldap/schema')
		{
			$slapd = '/usr/sbin/slapd';
			$ldap_schema_dir = '/etc/openldap/schema';
		}
		else
		{
			$setup_error = "OpenLDAP server installation not found";
			$setup = 0;
		}
	}
	elsif ($^O eq 'freebsd')
	{
		if (-d '/usr/local/etc/openldap/schema')
		{
			$slapd = '/usr/local/libexec/slapd';
			$ldap_schema_dir = '/usr/local/etc/openldap/schema';
		}
		else
		{
			$setup_error = "OpenLDAP server installation not found";
			$setup = 0;
		}
	}
	elsif ($^O eq 'openbsd')
	{
		if (-d '/usr/local/share/examples/openldap/schema')
		{
			$slapd = '/usr/local/libexec/slapd';
			$ldap_schema_dir = '/usr/local/share/examples/openldap/schema';
		}
		else
		{
			$setup_error = "OpenLDAP server installation not found";
			$setup = 0;
		}
	}
	else
	{
		$setup_error = "ldap tests not supported on $^O";
		$setup = 0;
	}
}

END
{
	# take care not to change the script's exit value
	my $exit_code = $?;

	foreach my $server (@servers)
	{
		next unless -f $server->{pidfile};
		my $pid = slurp_file($server->{pidfile});
		chomp $pid;
		kill 'INT', $pid;
	}

	$? = $exit_code;
}

=pod

=head1 METHODS

=over

=item LdapServer->new($rootpw, $auth_type)

Create a new LDAP server.

The rootpw can be used when authenticating with the ldapbindpasswd option.

The auth_type is either 'users' or 'anonymous'.

=back

=cut

sub new
{
	die "no suitable binaries found" unless $setup;

	my $class = shift;
	my $rootpw = shift;
	my $authtype = shift;    # 'users' or 'anonymous'
	my $testname = basename((caller)[1], '.pl');
	my $self = {};

	my $test_temp = PostgreSQL::Test::Utils::tempdir("ldap-$testname");

	my $ldap_datadir = "$test_temp/openldap-data";
	my $slapd_certs = "$test_temp/slapd-certs";
	my $slapd_pidfile = "$test_temp/slapd.pid";
	my $slapd_conf = "$test_temp/slapd.conf";
	my $slapd_logfile =
	  "${PostgreSQL::Test::Utils::log_path}/slapd-$testname.log";
	my $ldap_server = 'localhost';
	my $ldap_port = PostgreSQL::Test::Cluster::get_free_port();
	my $ldaps_port = PostgreSQL::Test::Cluster::get_free_port();
	my $ldap_url = "ldap://$ldap_server:$ldap_port";
	my $ldaps_url = "ldaps://$ldap_server:$ldaps_port";
	my $ldap_basedn = 'dc=example,dc=net';
	my $ldap_rootdn = 'cn=Manager,dc=example,dc=net';
	my $ldap_rootpw = $rootpw;
	my $ldap_pwfile = "$test_temp/ldappassword";

	(my $conf = <<"EOC") =~ s/^\t\t//gm;
		include $ldap_schema_dir/core.schema
		include $ldap_schema_dir/cosine.schema
		include $ldap_schema_dir/nis.schema
		include $ldap_schema_dir/inetorgperson.schema

		pidfile $slapd_pidfile
		logfile $slapd_logfile

		access to *
		        by * read
		        by $authtype auth

		database ldif
		directory $ldap_datadir

		TLSCACertificateFile $slapd_certs/ca.crt
		TLSCertificateFile $slapd_certs/server.crt
		TLSCertificateKeyFile $slapd_certs/server.key

		suffix "dc=example,dc=net"
		rootdn "$ldap_rootdn"
		rootpw "$ldap_rootpw"
EOC
	append_to_file($slapd_conf, $conf);

	mkdir $ldap_datadir or die "making $ldap_datadir: $!";
	mkdir $slapd_certs or die "making $slapd_certs: $!";

	my $certdir = dirname(__FILE__) . "/../ssl/ssl";

	copy "$certdir/server_ca.crt", "$slapd_certs/ca.crt"
	  || die "copying ca.crt: $!";
	# check we actually have the file, as copy() sometimes gives a false success
	-f "$slapd_certs/ca.crt" || die "copying ca.crt (error unknown)";
	copy "$certdir/server-cn-only.crt", "$slapd_certs/server.crt"
	  || die "copying server.crt: $!";
	copy "$certdir/server-cn-only.key", "$slapd_certs/server.key"
	  || die "copying server.key: $!";

	append_to_file($ldap_pwfile, $ldap_rootpw);
	chmod 0600, $ldap_pwfile or die "chmod on $ldap_pwfile";

	# -s0 prevents log messages ending up in syslog
	system_or_bail $slapd, '-f', $slapd_conf, '-s0', '-h',
	  "$ldap_url $ldaps_url";

	# wait until slapd accepts requests
	my $retries = 0;
	while (1)
	{
		last
		  if (
			system_log(
				"ldapsearch", "-sbase",
				"-H", $ldap_url,
				"-b", $ldap_basedn,
				"-D", $ldap_rootdn,
				"-y", $ldap_pwfile,
				"-n", "'objectclass=*'") == 0);
		die "cannot connect to slapd" if ++$retries >= 300;
		note "waiting for slapd to accept requests...";
		Time::HiRes::usleep(1000000);
	}

	$self->{pidfile} = $slapd_pidfile;
	$self->{pwfile} = $ldap_pwfile;
	$self->{url} = $ldap_url;
	$self->{s_url} = $ldaps_url;
	$self->{server} = $ldap_server;
	$self->{port} = $ldap_port;
	$self->{s_port} = $ldaps_port;
	$self->{basedn} = $ldap_basedn;
	$self->{rootdn} = $ldap_rootdn;

	bless $self, $class;
	push @servers, $self;
	return $self;
}

# private routine to set up the environment for methods below
sub _ldapenv
{
	my $self = shift;
	my %env = %ENV;
	$env{'LDAPURI'} = $self->{url};
	$env{'LDAPBINDDN'} = $self->{rootdn};
	return %env;
}

=pod

=over

=item ldapadd_file(filename)

filename is the path to a file containing LDIF data which is added to the LDAP
server.

=back

=cut

sub ldapadd_file
{
	my $self = shift;
	my $file = shift;

	local %ENV = $self->_ldapenv;

	system_or_bail 'ldapadd', '-x', '-y', $self->{pwfile}, '-f', $file;
}

=pod

=over

=item ldapsetpw(user, password)

Set the user's password in the LDAP server

=back

=cut

sub ldapsetpw
{
	my $self = shift;
	my $user = shift;
	my $password = shift;

	local %ENV = $self->_ldapenv;

	system_or_bail 'ldappasswd', '-x', '-y', $self->{pwfile}, '-s', $password,
	  $user;
}

=pod

=over

=item prop(name1, ...)

Returns the list of values for the specified properties of the instance, such
as 'url', 'port', 'basedn'.

=back

=cut

sub prop
{
	my $self = shift;
	my @settings;
	push @settings, $self->{$_} foreach (@_);
	return @settings;
}

1;