-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathmmr.pl
550 lines (445 loc) · 14.5 KB
/
mmr.pl
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
#!/usr/bin/perl -w
#
# mmr.pl - configure multimaster replication between two 389 servers
#
# Mike Jackson <[email protected]> 19.11.2005
# Federico Roman added the --port option. 10-08-2007
# Marc Sauton changed --port into --port1 and --port2 options. 15-10-2010
#
# Professional LDAP consulting for large and small projects
#
# http://www.netauth.com
#
# GPLv2 License
#
use strict;
use Getopt::Long;
use Net::LDAP qw(LDAP_ALREADY_EXISTS LDAP_TYPE_OR_VALUE_EXISTS);
use Pod::Usage;
my %o;
GetOptions(
\%o,
'base=s', # optional, default to get_base()
'binddn=s', # optional, default to "cn=directory manager"
'bindpw=s',
'repmanpw=s',
'host1=s',
'host2=s',
'host1_id=i',
'host2_id=i',
'port1=i',
'port2=i',
'create',
'display',
'remove',
'with-ssl',
'help',
'man',
);
pod2usage(-verbose => 1) if ($o{help});
pod2usage(-verbose => 2) if ($o{man} );
pod2usage(-verbose => 1) if (! ($o{create} || $o{display} || $o{remove}) );
# mandatory in all cases
my $host1 = $o{host1};
my $host2 = $o{host2};
my $bindpw = $o{bindpw};
# mandatory in create case
my $create = $o{create};
my $host1_id = $o{host1_id};
my $host2_id = $o{host2_id};
my $repmanpw = $o{repmanpw};
# mandatory in display case
my $display = $o{display};
# mandatory in remove case
my $remove = $o{remove};
# optional in create case
my $with_ssl = $o{'with-ssl'};
my $scheme = ($with_ssl && "ldaps") || "ldap";
# optional in all cases
my $port1 = $o{port1} || ($with_ssl && "636") || "389";
my $port2 = $o{port2} || ($with_ssl && "636") || "389";
my $base = $o{base} || get_base($host1, $port1);
my $binddn = $o{binddn} || "cn=directory manager";
# all cases check
if (!($host1 && $host2 && $bindpw))
{
pod2usage(-verbose => 1);
exit(1);
}
###############################################
# create
###############################################
if ($create)
{
if (!($host1_id && $host2_id && $repmanpw))
{
pod2usage(-verbose => 1);
exit(1);
}
else
{
# configure suppliers
config_supplier($host1, $port1, $host1_id, $repmanpw);
config_supplier($host2, $port2, $host2_id, $repmanpw);
# add replication agreements
add_rep_agreement($host1, $port1, $host2, $port2, $repmanpw);
add_rep_agreement($host2, $port2, $host1, $port1, $repmanpw);
# initialize host2 from host1
initialize($host1, $port1, $host2, $port2);
}
}
###############################################
# remove
###############################################
if ($remove)
{
if (!($host1 && $host2))
{
pod2usage(-verbose => 1);
exit(1);
}
else
{
# remove agreements
remove_agreement($host1, $port1, $host2);
remove_agreement($host2, $port2, $host1);
}
}
###############################################
# display
###############################################
if ($display)
{
if (!($host1 && $host2))
{
pod2usage(-verbose => 1);
exit(1);
}
else
{
# display agreements
print "\n";
display_agreement($host1, $port1);
print "\n";
display_agreement($host2, $port2);
print "\n";
}
}
###############################################
# subs
###############################################
sub display_agreement
{
my ($server, $prt) = @_;
my $msg;
my $res;
my $ldap = Net::LDAP->new("$scheme://$server", port => $prt) || die "$@";
$msg = $ldap->bind($binddn, password => $bindpw, version => 3);
$msg->code && die $msg->error;
print "replication agreements from $server port $prt\n";
$res = $ldap->search(
base => "cn=config",
filter => "(objectClass=nsDS5ReplicationAgreement)",
);
$res->code && die $res->error;
if ($res->count)
{
for ($res->entries)
{
print "\t -> to host " . $_->get_value("nsDS5ReplicaHost");
print " port " . $_->get_value("nsDS5ReplicaPort");
print " suffix " . $_->get_value("nsDS5ReplicaRoot") . "\n";
}
}
else
{
print "\t -> none found\n";
}
$ldap->unbind;
}
sub remove_agreement
{
my ($from, $prt, $to) = @_;
my $msg;
my $res;
my $ldap = Net::LDAP->new("$scheme://$from", port => $prt) || die "$@";
$msg = $ldap->bind($binddn, password => $bindpw, version => 3);
$msg->code && die $msg->error;
print "removing replication agreement from $from (port $prt) -> $to\n";
$res = $ldap->search(
base => "cn=config",
filter => "(&(objectClass=nsDS5ReplicationAgreement)(nsDS5ReplicaHost=$to))",
);
$res->code && die $res->error;
my $dn = $res->entry(0)->dn;
$res = $ldap->delete($dn);
$res->code && warn "failed to remove replication agreement: " . $res->error;
$ldap->unbind;
}
sub config_supplier
{
my ($server, $prt, $replicaid, $repmanpw) = @_;
my $msg;
my $res;
my $ldap = Net::LDAP->new("$scheme://$server", port => $prt) || die "$@";
$msg = $ldap->bind($binddn, password => $bindpw, version => 3);
$msg->code && die $msg->error;
##############################
# find the instance-dir
##############################
$res = $ldap->search (
base => "cn=config",
scope => "base",
filter => "(objectClass=*)",
);
my $instance_dir = $res->entry(0)->get_value("nsslapd-instancedir");
my $hadd = $server;
# $hadd = "newserver" if $server eq "some_exception";
$instance_dir ||= "/etc/dirsrv/slapd-$hadd";
##############################
##############################
# add changelog
##############################
print "adding to $server port $prt -> cn=changelog5,cn=config\n";
$res = $ldap->add(
"cn=changelog5,cn=config",
attr => [
objectclass => [qw (top extensibleObject)],
cn => "changelog5",
"nsslapd-changelogdir" => "$instance_dir/changelogdb",
]
);
if ($res->code == LDAP_ALREADY_EXISTS)
{
print "\t -> already exists\n\n";
}
else
{
$res->code && die "failed to add changelog entry: " . $res->error;
}
##############################
##############################
# add replication user
##############################
print "adding to $server port $prt -> cn=repman,cn=config\n";
$res = $ldap->add(
"cn=repman,cn=config",
attr => [
objectclass => [qw (top person)],
cn => "repman",
sn => "repman",
userPassword => $repmanpw,
]
);
if ($res->code == LDAP_ALREADY_EXISTS)
{
print "\t -> already exists\n\n";
}
else
{
$res->code && die "failed to add repman entry: " . $res->error;
}
##############################
##############################
# add replica object
##############################
print "adding to $server port $prt -> cn=replica,cn=\"$base\",cn=mapping tree,cn=config\n";
$res = $ldap->add(
"cn=replica,cn=\"$base\",cn=mapping tree,cn=config",
attr => [
objectclass => [qw (top nsDS5Replica)],
cn => "replica",
nsDS5ReplicaId => $replicaid,
nsDS5ReplicaRoot => $base,
nsDS5Flags => 1,
nsDS5ReplicaBindDN => "cn=repman,cn=config",
nsds5ReplicaPurgeDelay => 604800,
nsds5ReplicaLegacyConsumer => "off",
nsDS5ReplicaType => 3,
]
);
if ($res->code == LDAP_ALREADY_EXISTS)
{
print "\t -> already exists\n\n";
}
else
{
$res->code && die "failed to add replica entry: " . $res->error;
}
##############################
$ldap->unbind;
}
sub add_rep_agreement
{
my ($from, $prt1, $to, $prt2, $repmanpw) = @_;
my $msg;
my $res;
my $ldap = Net::LDAP->new("$scheme://$from", port => $prt1) || die "$@";
$msg = $ldap->bind($binddn, password => $bindpw, version => 3);
$msg->code && die $msg->error;
if ($with_ssl)
{
print "adding to $from port $prt1 -> SSL replication $from port $prt1 -> $to port $prt2\n";
$res = $ldap->add(
"cn=\"Replication from $from port $prt1 to $to port $prt2\",cn=replica,cn=\"$base\",cn=mapping tree,cn=config",
attr => [
objectclass => [qw (top nsDS5ReplicationAgreement)],
cn => "\"Replication from $from port $prt1 to $to port $prt2\"",
nsDS5ReplicaHost => $to,
nsDS5ReplicaRoot => "$base",
nsDS5ReplicaPort => $prt2,
nsDS5ReplicaTransportInfo => "SSL",
nsDS5ReplicaBindDN => "cn=repman,cn=config",
nsDS5ReplicaBindMethod => "simple",
nsDS5ReplicaCredentials => $repmanpw,
nsDS5ReplicaTimeOut => 120,
]
);
if ($res->code == LDAP_ALREADY_EXISTS)
{
print "\t -> already exists\n\n";
}
else
{
$res->code && die "failed to add replication agreement entry: " . $res->error;
}
}
else
{
print "adding to $from port $prt1 -> plaintext replication $from port $prt1 -> $to port $prt2\n";
$res = $ldap->add(
"cn=\"Replication from $from port $prt1 to $to port $prt2\",cn=replica,cn=\"$base\",cn=mapping tree,cn=config",
attr => [
objectclass => [qw (top nsDS5ReplicationAgreement)],
cn => "\"Replication from $from port $prt1 to $to port $prt2\"",
nsDS5ReplicaHost => $to,
nsDS5ReplicaRoot => "$base",
nsDS5ReplicaPort => $prt2,
nsDS5ReplicaBindDN => "cn=repman,cn=config",
nsDS5ReplicaBindMethod => "simple",
nsDS5ReplicaCredentials => $repmanpw,
nsDS5ReplicaTimeOut => 120,
]
);
if ($res->code == LDAP_ALREADY_EXISTS)
{
print "\t -> already exists\n\n";
}
else
{
$res->code && die "failed to add replication agreement entry: " . $res->error;
}
}
$ldap->unbind;
}
sub initialize
{
my ($from, $prt1, $to, $prt2) = @_;
my $msg;
my $res;
my $ldap = Net::LDAP->new("$scheme://$from", port => $prt1) || die "$@";
$msg = $ldap->bind($binddn, password => $bindpw, version => 3);
$msg->code && die $msg->error;
print "initializing replication $from (port $prt1) -> $to (port $prt2)\n";
my $dn = "cn=\"Replication from $from port $prt1 to $to port $prt2\",cn=replica,cn=\"$base\",cn=mapping tree,cn=config";
$res = $ldap->modify($dn, add => { nsDS5BeginReplicaRefresh => 'start' });
$res->code && die "failed to add initialization attribute: " . $res->error;
$ldap->unbind;
}
sub get_base
{
my ($server, $prt) = @_;
my $base;
my $res;
my $ldap = Net::LDAP->new("$scheme://$server", port => $prt) || die "$@";
$ldap->bind;
$res = $ldap->search(
base => "",
scope => "base",
filter => "(objectClass=*)",
);
$res->code && die $res->error;
my @contexts = $res->entry(0)->get_value("namingContexts");
for (@contexts)
{
if (! /o=NetscapeRoot/)
{
$base = $_;
}
}
$ldap->unbind;
return $base;
}
__END__
=head1 NAME
mmr.pl - Configure multi-master replication between two 389 Directory Servers
=head1 SYNOPSIS
Usage: mmr.pl [options]
Mandatory in all cases:
--host1 FQDN of host 1
--host2 FQDN of host 2
--bindpw Password for Directory Manager
Optional in all cases:
--base LDAP naming context
--binddn Alternative distinguished name for Directory Manager
--port1 LDAP port (default=389) for host 1
--port2 LDAP port (default=389) for host 2
Mandatory for creating a 2-way agreement:
--host1_id Replication ID number of host 1
--host2_id Replication ID number of host 2
--repmanpw Password for Replication Manager
--create
Optional when creating a 2-way agreement:
--with-ssl Use SSL for Replication (requires CA and server certs on both machines)
Mandatory for removing a 2-way agreement:
--remove
Mandatory for displaying agreements
--display
Examples:
# create a 2-way replication agreement
% mmr.pl \
--host1 a.bigcorp.com \
--host2 b.bigcorp.com \
--host1_id 1 \
--host2_id 2 \
--bindpw secret \
--repmanpw repsecret \
--create
# create a 2-way replication agreement with SSL
% mmr.pl \
--host1 a.bigcorp.com \
--host2 b.bigcorp.com \
--host1_id 1 \
--host2_id 2 \
--bindpw secret \
--repmanpw repsecret \
--create \
--with-ssl
# remove a 2-way replication agreement
% mmr.pl \
--host1 a.bigcorp.com \
--host2 b.bigcorp.com \
--bindpw secret \
--remove
# display replication agreements
% mmr.pl \
--host1 a.bigcorp.com \
--host2 b.bigcorp.com \
--port1 3890
--bindpw secret \
--display
=head1 DESCRIPTION
B<mmr.pl> will configure multi-master replication between two 389 or
Red Hat Directory Servers.
The listen port must be the same on both machines. By default port 389
is assumed (636 if --with-ssl) , but it can be overriden with the --port option.
The "cn=Directory Manager" user's password must be the same on both
machines. As well, the "cn=Directory Manager" user which was created
during installation must exist on both machines, or if this username
was changed to something else then it can be provided with the --binddn
option.
The LDAP naming context must be the same on both machines. If there
is only one naming context configured on the machines, then it is not
necessary to provide the --base option as the naming context will be
discovered automatically.
=cut