summaryrefslogtreecommitdiffstats
path: root/ldap/admin/src/migrateTo4
blob: dcf63eb52501a5e69a1f7ea309a6ca3b06cda75c (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
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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
#!perl
#
# BEGIN COPYRIGHT BLOCK
# This Program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; version 2 of the License.
# 
# This Program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License along with
# this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
# Place, Suite 330, Boston, MA 02111-1307 USA.
# 
# In addition, as a special exception, Red Hat, Inc. gives You the additional
# right to link the code of this Program with code not covered under the GNU
# General Public License ("Non-GPL Code") and to distribute linked combinations
# including the two, subject to the limitations in this paragraph. Non-GPL Code
# permitted under this exception must only link to the code of this Program
# through those well defined interfaces identified in the file named EXCEPTION
# found in the source code files (the "Approved Interfaces"). The files of
# Non-GPL Code may instantiate templates or use macros or inline functions from
# the Approved Interfaces without causing the resulting work to be covered by
# the GNU General Public License. Only Red Hat, Inc. may make changes or
# additions to the list of Approved Interfaces. You must obey the GNU General
# Public License in all respects for all of the Program code and other code used
# in conjunction with the Program except the Non-GPL Code covered by this
# exception. If you modify this file, you may extend this exception to your
# version of the file, but you are not obligated to do so. If you do not wish to
# do so, delete this exception statement from your version. 
# 
# 
# Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
# Copyright (C) 2005 Red Hat, Inc.
# All rights reserved.
# END COPYRIGHT BLOCK
#

BEGIN {
	require 'uname.lib';
	$isNT = -d '\\';
	@INC = ( '.', '../../../admin/admin/bin' );
	grep { s@/@\\@g } @INC if $isNT;
	$PATHSEP = $isNT ? '\\' : '/';
	# NT needs quotes around some things unix doesn't
	$quote = $isNT ? "\"" : "";

	# If this variable is set, all file/directory creation will make sure the mode
	# and ownership of the destination is the same as the source
	$PRESERVE = 1 if (!$isNT);
	$script_suffix = $isNT ? ".bat" : "";
	$exe_suffix = $isNT ? ".exe" : "";
    if ($isNT) {
		$os = "WINNT";
    } else {
    	$os = &uname("-s");
    }

	if ($isNT) {
		# we have to pass batch files directly to the NT command interpreter
		$com_spec = $ENV{ComSpec};
		if (!$com_spec) {
			$com_spec = $ENV{COMSPEC};
		}
		if (!$com_spec || ! -f $com_spec) {
			# find the first available command interpreter
			foreach $drive (c..z) {
				$com_spec = "$drive:\\winnt\\system32\\cmd.exe";
				last if (-f $com_spec);
				$com_spec = undef;
			}
			if (! $com_spec) {
				# punt and pray
				$com_spec = 'c:\winnt\system32\cmd.exe';
			}
		}
	}

	# dll suffix for shared libraries in old instance; note that the dll suffix
	# may have changed for the new instance e.g. AIX now uses .so
    if ( $os eq "AIX" ) {
		$dll_suffix = "_shr.a";
    }	
    elsif ( $os eq "HP-UX" ) {
	$arch = &uname("-p");
	if ( $arch eq "ia64" ) {
		$dll_suffix = ".so";
	} else {
		$dll_suffix = ".sl";
	}
    }	
    elsif ( $os eq "WINNT" ) {
		$dll_suffix = ".dll";
    }	
    else {
		$dll_suffix = ".so";
    }	
	$slapdExecName = $isNT ? 'slapd.exe' : 'ns-slapd';
	# if this flag is set, we will migrate the 3.0 and 3.1 databases
	# by doing a db2ldif -> ldif2db; if this is not set, we will just
	# copy the directories; right now, we cannot copy the directories,
	# because the database format has changed for 4.0, and the new
	# code does not recognize the old db format.  It is hoped that it
	# will by RTM . . .
	$convertToLDIF = 1;
	select STDERR;
	$| = 1;
	select STDOUT;
	$| = 1;

	# if the old value for dbcachesize is less than this, make it this
	$MIN_DBCACHESIZE = '500000';
}

sub getCwd {
	my $command = $isNT ? "cd" : "/bin/pwd";
	open(PWDCMD, "$command 2>&1 |") or
		die "Error: could not execute $command: $!";
	# without the following sleep, reading from the pipe will
	# return nothing; I guess it gives the pwd command time
	# to get some data to read . . .
	sleep(1);
	my $curdir;
	while (<PWDCMD>) {
		if (!$curdir) {
			chomp($curdir = $_);
		}
	}
	my $code = close(PWDCMD);
#	if ($code || $?) {
#		print "$command returned code=$code status=$? dir=$curdir\n";
#	}
#	print "getCwd curdir=\[$curdir\]\n";
	return $curdir;
}

sub fixBinaryAttr {
	my $foo = shift;
	$foo =~ s/;binary//ig;
	return $foo;
}

$type = "slapd";
$root = $ARGV[0];
$oldDir = $ARGV[1];
$newname = $ARGV[2];
$oldname = $ARGV[3];
$savedMDLdif = $ARGV[4];
$savedLdif = $ARGV[5];
$sieName = $ARGV[6];
$secPwd = $ARGV[7];

if (($root =~ m#/$#) || ($root =~ m#\\$#)) {
	chop $root;
}

if (($oldDir =~ m#/$#) || ($oldDir =~ m#\\$#)) {
	chop $oldDir;
}

sub basename {
	my @list = split(/[\\\/]/, $_[0]);
	return $list[@list - 1];
}

# this is used to strip html formatting from output to user
sub localprint {
	# arg 1 is string to print
	# arg 2 is beginning html directive
	# arg 3 is closing html directive
	my ($str, $begin, $end) = @_;
	print $str;
}

# this is used to run the system() call, capture exit and signal codes,
# and die() upon badness; the first argument is a directory to change
# dir to, if any, and the rest are passed to system()
sub mySystem {
	my $rc = &mySystemNoDie(@_);
	my ($dir, @args) = @_;
    if ($rc == 0) {
# success
    } elsif ($rc == 0xff00) {
		die "Error executing @args: error code $rc: $!";
    } elsif ($rc > 0x80) {
        $rc >>= 8;
		die "Error executing @args: error code $rc: $!";
    } else {
        if ($rc &   0x80) {
            $rc &= ~0x80;
        } 
		die "Error executing @args: received signal $rc: $!";
    }

	# usually won't get return value
	return $rc;
}

# This version does not die but just returns the error code
sub mySystemNoDie {
	my ($dir, @args) = @_;
	if ($dir && ($dir ne "")) {
		chdir($dir) or die "Could not change directory to $dir: $!";
	}
	my $cmd = $args[0];
	# the system {$cmd} avoids some NT shell quoting problems if the $cmd
	# needs to be quoted e.g. contains spaces; the map puts double quotes
	# around the arguments on NT which are stripped by the command
	# interpreter cmd.exe; but don't quote things which are already quoted
	my @fixargs = map { /^[\"].*[\"]$/ ? $_ : $quote . $_ . $quote } @args;
	my $rc = 0;
	if ($cmd =~ /[.](bat|cmd)$/) {
		# we have to pass batch files directly to the NT command interpreter
		$cmd = $com_spec;
#		print "system $cmd /c \"@fixargs\"\n";
		$rc = 0xffff & system {$cmd} '/c', "\"@fixargs\"";
	} else {
#		print "system $cmd @fixargs\n";
        $rc = 0xffff & system {$cmd} @fixargs;
    }
	return $rc;
}

$serverHome = "$root${PATHSEP}$type-$newname";

$oldHome = "$oldDir${PATHSEP}slapd-$oldname";

# these are the default values used by the 4.0 installer
$DEFAULT_CHANGELOG_DIR = $serverHome . $PATHSEP . 'logs' . $PATHSEP . 'changelogdb';
$DEFAULT_CHANGELOG_SUFFIX = "cn=changelog";

# get some information from the new slapd.conf file
open(INPUT, "$serverHome${PATHSEP}config${PATHSEP}slapd.conf") or
	die "Could not open file $serverHome${PATHSEP}config${PATHSEP}slapd.conf";
while (<INPUT>) {
	if (/^port\s+/i) { chomp($newport = $'); }
	elsif (/^localhost\s+/i) { chomp($newlocalhost = $'); }
	elsif (/^localuser\s+/i) { chomp($newuser = $'); }
}
close INPUT;

# get some information from the new slapd.ldbm.conf file
open(INPUT, "$serverHome${PATHSEP}config${PATHSEP}slapd.ldbm.conf") or
	die "Could not open file $serverHome${PATHSEP}config${PATHSEP}slapd.ldbm.conf";
while (<INPUT>) {
	if (/^directory\s+[\"]?(.*?)[\"]?\s*$/i) {
		# "
		$newDbDir = $1;
		# paths are stored in unix format in the config files . . .
		$newDbDir =~ s#/#\\#g if ($isNT);
	}
}
close INPUT;

# get some information from the old slapd.conf file
open(INPUT, "$oldHome${PATHSEP}config${PATHSEP}slapd.conf") or
	die "Could not open file $oldHome${PATHSEP}config${PATHSEP}slapd.conf";
while (<INPUT>) {
	if (/^changelogdir\s+[\"]?(.*?)[\"]?\s*$/i) {
		# "
		$oldChangeLogDir = $1;
		# paths are stored in unix format in the config files . . .
		$oldChangeLogDir =~ s#/#\\#g if ($isNT);
	}
	elsif (/^changelogsuffix\s+[\"]?(.*?)[\"]?\s*$/i) {
		# "
		$oldChangeLogSuffix = $1;
	}
	elsif (/^directory\s+[\"]?(.*?)[\"]?\s*$/i) {
		# "
		$oldDbDir = $1;
		# paths are stored in unix format in the config files . . .
		$oldDbDir =~ s#/#\\#g if ($isNT);
	}
	elsif (/^localuser\s+/i) { chomp($olduser = $'); }
	elsif (/^encryption-alias\s+/i) { chomp($encryption_alias = $'); }
	# the user may have given us a network mounted old home directory, but in the
	# old instance's config files, the root directory referred to is usually
	# a local directory.  For example, suppose there is an automounter map for
	# hosts which maps onto /h e.g. /h/oldhost would contain all directories
	# exported via NFS.  Similarly, for NT, you could do \\oldhost\c to look
	# at the C: drive on the old host.  Or the user may have network mounted
	# the old server root some other way.  Anyway, we need to determine what
	# the old server root was local to the original host because that is what
	# will be referred to it the old config files.  So, we look at the errorlog
	# directive in slapd.conf and use whatever comes before the slapd-oldname
	elsif (/^errorlog\s+[\"]?(.*)$type-$oldname/i) {
		# there may be leading "
		chop($realOldDir = $1);
	}
}
close INPUT;

if (! $realOldDir) {
	$realOldDir = $oldDir;
}

$realOldHome = $realOldDir . $PATHSEP . $type . '-' . $oldname;

# the oldDbDir is stored as a local dir, but we may need a network dir
($networkDbDir = $oldDbDir) =~ s/^$realOldDir/$oldDir/ig;

# list of standard plugins configured out of the box in version 3
# all of these paths are in unix format . . .
$oldLibDir = lc("$realOldDir/lib/");
$oldConfDir = lc("$realOldHome/config/");
$oldLogsDir = lc("$realOldHome/logs/");
$oldLibDir =~ s#\\#/#g if ($isNT);
$oldConfDir =~ s#\\#/#g if ($isNT);
$oldLogsDir =~ s#\\#/#g if ($isNT);

# note that all of these should be lower case, since NT does not distinguish case
# and we don't really care about case for plugin directives anyway . . .
%stdPlugins = (
	"plugin syntax \"${oldLibDir}syntax-plugin${dll_suffix}\" cis_init", "\n",
	"plugin syntax \"${oldLibDir}syntax-plugin${dll_suffix}\" ces_init", "\n",
	"plugin syntax \"${oldLibDir}syntax-plugin${dll_suffix}\" bin_init", "\n",
	"plugin syntax \"${oldLibDir}syntax-plugin${dll_suffix}\" tel_init", "\n",
	"plugin syntax \"${oldLibDir}syntax-plugin${dll_suffix}\" int_init", "\n",
	"plugin syntax \"${oldLibDir}syntax-plugin${dll_suffix}\" dn_init", "\n",
	"plugin matchingrule \"${oldLibDir}liblcoll${dll_suffix}\" orderingrule_init ${quote}${oldConfDir}slapd-collations.conf$quote", "\n",
	"plugin database \"${oldLibDir}libback-ldbm${dll_suffix}\" ldbm_back_init", "\n",
	"plugin postoperation ${quote}${oldLibDir}referint-plugin${dll_suffix}${quote} referint_postop_init 0 ${quote}${oldLogsDir}referint${quote} member uniquemember owner seealso", "\n",
	"plugin postoperation ${quote}${oldLibDir}referint-plugin${dll_suffix}${quote} referint_postop_init 0 ${quote}${oldLogsDir}referint${quote} 0 member uniquemember owner seealso", "\n",
	"plugin preoperation ${quote}${oldLibDir}libntsynch${dll_suffix}${quote} libntsynch_plugin_preop_init", "\n",
	"plugin postoperation ${quote}${oldLibDir}libntsynch${dll_suffix}${quote} libntsynch_plugin_postop_init", "\n"
);

# list of standard indexes configured out of the box in version 3
%stdIndex = (
	'index   aci pres', "\n",
	'index   cn pres,eq,sub', "\n",
	'index   sn pres,eq,sub', "\n",
	'index   givenName pres,eq,sub', "\n",
	'index   mail pres,eq,sub', "\n",
	'index   telephoneNumber pres,eq,sub', "\n",
	'index   ntUserDomainId pres,eq,sub', "\n",
	'index   uid eq', "\n",
	'index   changenumber eq', "\n",
	'index   uniquemember eq', "\n",
	'index   member eq', "\n",
	'index   owner  eq', "\n",
	'index   seeAlso eq', "\n"
);

# These are files included into slapd.conf, slapd.dynamic-ldbm.conf and
# slapd.ldbm.conf by default in earlier releases.  We use this hash to
# determine if there are user defined files which have been included
# into the slapd.conf e.g. for user defined attributes, object classes,
# indexes, etc.
%stdIncludes = (
	"${oldConfDir}slapd.at.conf", "\n",
	"${oldConfDir}slapd.oc.conf", "\n",
	"${oldConfDir}ns-schema.conf", "\n",
	"${oldConfDir}ns-globopt.conf", "\n",
);


# list of parameters that we don't care about; these are usually just parameters
# which hold paths relative to this instance and server root, which change anyway
%oldParametersToSkip = (
	'userat', "\n", # use the new one
	'useroc', "\n", # use the new one
	'instancedir', "\n", # must be the new one
	'dynamicconf', "\n", # use the new one
	'directory', "\n", # use the new one
	'access', "\n", # obsolete
	'defaultaccess', "\n", # obsolete
	'security-path', "\n", # obsolete
	'localuser', "\n", # use the newly configured suitespot user
	'port', "\n", # the new port must already be set either as determined from
				  # the old config or because we are migrating into the MC
				  # instance and cannot change the port number
	'rootdn', "\n", # the new rootdn must already be set either as determined from
				  # the old config or because we are migrating into the MC
				  # instance and cannot change it
	'rootpw', "\n", # the new rootpw must already be set either as determined from
				  # the old config or because we are migrating into the MC
				  # instance and cannot change it
);

# list of old ldbm specific parameters.  These parameters may be present in the
# old slapd.conf, but have been moved to the new slapd.ldbm.conf
%oldLdbmParameters = (
	'database', "\n",
	'lookthroughlimit', "\n",
	'mode', "\n",
	'cachesize', "\n",
	'dbcachesize', "\n",
	'allidsthreshold', "\n",
	'parentcheck', "\n",
);

# list of old slapd.conf parameters which have been moved to the new dse.ldif
%oldDSEParameters = (
	'encryption-alias', "\n",
	'sslclientauth', "\n"
);

($oldversion,$oldminor) = &getVersion($oldDir);
($newversion,$newminor) = &getVersion($root);

# if there was no old user specified
if (! $isNT && ! $olduser) {
	# get the olduid and oldgid from doing a stat of the db directory
	($olduid, $oldgid) = (stat($networkDbDir))[4..5];
}
# convert the user names to numeric uids
if ($PRESERVE) {
	if (! $olduid && $olduser) {
		($login,$pass,$olduid,$oldgid) = getpwnam($olduser);
	}
	($login,$pass,$newuid,$newgid) = getpwnam($newuser);
}

# copy the old config files
&copyDir("$oldHome${PATHSEP}config", "$serverHome${PATHSEP}migrate_config");

print "Migrating log files . . .\n";
# copy the log files
$srcdir = "$oldHome${PATHSEP}logs";
opendir(LOGDIR, $srcdir) or
	die "Error: could not open log file dir $srcdir : $!";
foreach (readdir(LOGDIR)) {
	if (! /[.][.]?/ && -f "$srcdir${PATHSEP}$_") {
		&copyBinFile("$srcdir${PATHSEP}$_",
					 "$serverHome${PATHSEP}logs${PATHSEP}${_}.migrate");
	}
}
closedir(LOGDIR);

# copy the ssl directory
&copyDir("$oldHome${PATHSEP}ssl", "$serverHome${PATHSEP}ssl");

# copy the cert db and key files
if ( -d "$oldDir${PATHSEP}alias" && $encryption_alias ) {
	$aliasDir = "$root${PATHSEP}alias";
	if (! -d $aliasDir) {
		mkdir($aliasDir, 0750);
	}
	$adminDir = $root . $PATHSEP . 'bin' . $PATHSEP . 'admin' . $PATHSEP .
		'admin' . $PATHSEP . 'bin';
	print "Migrating the key and certificate databases . . .\n";
	mySystem($adminDir, $adminDir . $PATHSEP . 'sec-migrate',
			 $oldDir, $encryption_alias, $root, $sieName, $secPwd);
	# copy the old password file
	if (-f "$oldDir${PATHSEP}alias${PATHSEP}$encryption_alias-password.txt") {
		&copyBinFile(
			 "$oldDir${PATHSEP}alias${PATHSEP}$encryption_alias-password.txt",
			 "$aliasDir${PATHSEP}$type-$newname-password.txt"
		);
		if ($newversion >= 4 && $newminor >= 1) {
			# need to convert the old format to new pin format
			print "Converting password file to new pin format . . .\n";
			$script = "$root${PATHSEP}bin${PATHSEP}slapd${PATHSEP}admin${PATHSEP}bin${PATHSEP}migratePwdFile";
			mySystem($aliasDir, $, $script, $root, "$type-$newname");
		}
	}

	# get the new key/cert db filenames
	opendir(CERTDIR, $aliasDir) or
		die "Error: could not open cert dir $aliasDir: $!";
	foreach (readdir(CERTDIR)) {
		if (/^$sieName/i) {
			if (/[-]cert/) {
				$newcertdb = $_;
			} elsif (/[-]key/) {
				$newkeydb = $_;
			}
		}
	}
	closedir(CERTDIR);
}

$needAclUpg = 0;
if ($oldversion == 1) {
	$needAclUpg = 1;
	$convertToLDIF = 1; # always need this for conversion from 1.X db
}

# Copy/Convert ldif files in ldif/
print "Migrating old LDIF files . . .\n";
&copyLdif;

if ($convertToLDIF) {
	# Converting database
	print "Migrating database to LDIF . . .\n";
	$oldLdif = "$oldHome${PATHSEP}ldif${PATHSEP}old.ldif";
	&db2ldif($networkDbDir, $oldLdif);
	if ($needAclUpg) {
		print "Converting ACLs in old data . . .\n";
		&mySystem("$root${PATHSEP}bin${PATHSEP}slapd${PATHSEP}server",
				  "$root${PATHSEP}bin${PATHSEP}slapd${PATHSEP}server${PATHSEP}" .
				  "aclupg$exe_suffix", '-d', '-i',
				  $oldLdif, '-o',
				  "$oldHome${PATHSEP}ldif${PATHSEP}aclupg.ldif");
		unlink($oldLdif);
		rename("$oldHome${PATHSEP}ldif${PATHSEP}aclupg.ldif", $oldLdif);
	}
	chown $newuid, $newgid, $oldLdif if (!$isNT);
# copy the changelogdb directory
# how to handle a 1.0 change log?
# &copyDir($changelogdir, "$serverHome${PATHSEP}changelogdb") if ($changelogdir);
}

# Compare each configuration file against its default version.  If it has changed,
# notify the user that the file has changed and will need to be checked by the
# user.  This should be safe to do because there should be no path information
# stored in these conf files, which are just schema stuff.
print "Migrating configuration files . . .\n";
$origFilePath = "$root${PATHSEP}bin${PATHSEP}slapd${PATHSEP}install${PATHSEP}version${oldversion}";
$srcdir = "$serverHome${PATHSEP}migrate_config";
opendir(CONFDIR, $srcdir) or
	die "Error: could not open migrated config dir $srcdir: $!";
foreach $file (readdir(CONFDIR)) {
	$origFile = $origFilePath . $file;
	if (-f $origFile) {
		$diffs = &diff("$srcdir${PATHSEP}$file", $origFile);
		if ($diffs) {
			print "File $srcdir${PATHSEP}$file could not be migrated\n";
			print "because it is different than\n";
			print "the standard installed version.  You will need to check this\n";
			print "file and make sure its changes are compatible with the new\n";
			print "directory server.  Here are the differences:\n";
			print $diffs, "\n";
		} else {
#			print "No changes to old config file $srcdir${PATHSEP}$file\n";
		}
	}
}
closedir(CONFDIR);

# make a backup of the current user_at and user_oc files, and copy the old ones
# into the config directory

&copyBinFile("$serverHome${PATHSEP}config${PATHSEP}slapd.user_at.conf",
			 "$serverHome${PATHSEP}config${PATHSEP}slapd.user_at.conf.bak");
&copyBinFile("$serverHome${PATHSEP}config${PATHSEP}slapd.user_oc.conf",
			 "$serverHome${PATHSEP}config${PATHSEP}slapd.user_oc.conf.bak");

if (-f "$serverHome${PATHSEP}migrate_config${PATHSEP}slapd.user_at.conf") {
	&copyAndEditTextFile(
		"$serverHome${PATHSEP}migrate_config${PATHSEP}slapd.user_at.conf",
		"$serverHome${PATHSEP}config${PATHSEP}slapd.user_at.conf",
		\&fixBinaryAttr);
}

if (-f "$serverHome${PATHSEP}migrate_config${PATHSEP}slapd.user_oc.conf") {
	&copyAndEditTextFile(
		"$serverHome${PATHSEP}migrate_config${PATHSEP}slapd.user_oc.conf",
		"$serverHome${PATHSEP}config${PATHSEP}slapd.user_oc.conf",
		\&fixBinaryAttr);
}

# parse the parameters from the old configuration files and put them into
# the new configuration files
&fixConf("$serverHome${PATHSEP}migrate_config${PATHSEP}slapd.conf",
	"$serverHome${PATHSEP}migrate_config${PATHSEP}slapd.dynamic_ldbm.conf",
	"$serverHome${PATHSEP}migrate_config${PATHSEP}dse.ldif",
	"$serverHome${PATHSEP}config${PATHSEP}slapd.conf",
	"$serverHome${PATHSEP}config${PATHSEP}slapd.ldbm.conf",
	"$serverHome${PATHSEP}config${PATHSEP}dse.ldif");

# copy in old data and any data we wanted to save
if ($convertToLDIF) {
	print "Migrating old database to new database . . .\n";
	&manyLdif2db($savedMDLdif, $oldLdif, $savedLdif);
	unlink $savedMDLdif, $savedLdif;
}

if ($oldChangeLogDir && -e $oldChangeLogDir) {
	print "Migrating changelog database . . .\n";
	my $realDir = $oldChangeLogDir;
	$realDir =~ s/^$realOldDir/$oldDir/ig;
	if ($convertToLDIF) {
		$srcDir = $realDir;
		$destDir = $DEFAULT_CHANGELOG_DIR;
		$srcLDIF = "$oldHome${PATHSEP}ldif${PATHSEP}changelog.ldif";
		$destLDIF = "$serverHome${PATHSEP}ldif${PATHSEP}changelog.ldif";
		mkdir( $destDir , 0755 ) if !( -e $destDir);
		# Converting database
		if ( !$isNT && $newuser ) {
			chown($newuid, $newgid, $destDir);
		}
		&other_db2ldif($srcDir, $srcLDIF);
		if ($needAclUpg) {
			&mySystem("$root${PATHSEP}bin${PATHSEP}slapd${PATHSEP}server",
					  "$root${PATHSEP}bin${PATHSEP}slapd${PATHSEP}server" .
					  "${PATHSEP}aclupg$exe_suffix", '-d', '-i',
					  $srcLDIF, '-o', $destLDIF);
		} else {
			&copyBinFile($srcLDIF, $destLDIF);
		}
		&other_ldif2db($destLDIF, $destDir, 'slapd.ldbm.conf',
					   "suffix \"$oldChangeLogSuffix\"");
	} else {
		# the dir is stored as a local dir, but we may need a network dir here
		&copyDir($realDir, $DEFAULT_CHANGELOG_DIR, '\.share$');
	}
}

if ($convertToLDIF) {
	# Convert the db backup, bak/
	print "Migrating database backups . . .\n";
	&copyBak;
} else {
	# just copy the directories over
	&copyDir($networkDbDir, "$serverHome${PATHSEP}db", '\.share$');
	&copyDir("$oldHome${PATHSEP}bak", "$serverHome${PATHSEP}bak", '\.share$');
}

if (-f $oldLdif) {
	unlink($oldLdif);
}

exit(0);

############# END OF PROCESSING; SUBROUTINES FOLLOW

# This subroutine merges the old and new source files into the new destination file
sub fixConf {
    my	$oldsrc = shift;
    my	$oldldbmsrc = shift;
    my	$olddseldif = shift;
    my	$newsrc = shift;
    my	$newldbmsrc = shift;
    my	$newdseldif = shift;

	# read the old conf file into a hash table
    open( OLDSRC, $oldsrc ) ||
		die "Can't open $oldsrc: $!: ";
	LINE: while ( <OLDSRC> ) {
		if (/^\s*#/) { # skip comments
			next LINE;
		}
		if (/^\s*$/) { # skip blank lines
			next LINE;
		} elsif (/^plugin/i) {
			chomp($_);
			if (! &isAStandardPlugin($_)) {
				push @badPlugins, $_;
			}
		} elsif (/^index/i) {
			chomp($_);
			if (! &isAStandardIndex($_)) {
				push @newIndex, $_;
			}
		} elsif (/^include\s+[\"]?(.*?)[\"]?\s*$/i) {
			# strip leading and trailing "
			if (! &isAStandardInclude($1)) {
				push @newInclude, $1;
			}
		} elsif (/^dbcachesize\s+[\"]?(.*?)[\"]?\s*$/i) {
			# strip leading and trailing "
			$param = 'dbcachesize';
			$value = $1;
			if ($value < $MIN_DBCACHESIZE) {
				$value = $MIN_DBCACHESIZE;
			}

			if ($oldLdbmParameters{lc($param)}) {
				$oldldbmhash{lc($param)} = $value;
			} else {
				$oldhash{lc($param)} = $value;

			}
		} elsif (/^errorlog/i) {
			$oldhash{'errorlog-logging-enabled'} = "on";
		} elsif (/^accesslog/i) {
			$oldhash{'accesslog-logging-enabled'} = "on";
		} elsif (/^auditlog/i) {
			$oldhash{'auditlog-logging-enabled'} = "on";
		} elsif (/^replogfile/i) {
			# replogfile was only used in 1.X, and it had no suffix
			$oldhash{'changelogdir'} = $DEFAULT_CHANGELOG_DIR;
			$oldhash{'changelogsuffix'} = $DEFAULT_CHANGELOG_SUFFIX;
		} elsif (/^changelogdir/i) {
			# force use of default
			$oldhash{'changelogdir'} = $DEFAULT_CHANGELOG_DIR;
		} elsif (/^\s*(\S+)\s+[\"]?(.*?)[\"]?\s*$/i) {
			# strip leading and trailing "
			$param = $1;
			$value = $2;
			if ($oldParametersToSkip{lc($param)}) {
				next LINE;
			} elsif (lc($param) eq 'suffix') {
				if (lc($value) cmp 'cn=schema') {
					$oldsuffix{lc($value)} = $value;
				}
			} else {
				if ($oldLdbmParameters{lc($param)}) {
					$oldldbmhash{lc($param)} = $value;
				} elsif ($oldDSEParameters{lc($param)}) {
					if (lc($param) eq 'encryption-alias') {
						if ($newcertdb) {
							$olddsehash{'nscertfile'} = "alias/$newcertdb";
						} else {
							$olddsehash{'nscertfile'} = "alias/$type-$newname-cert.db";
						}
						if ($newkeydb) {
							$olddsehash{'nskeyfile'} = "alias/$newkeydb";
						} else {
							$olddsehash{'nskeyfile'} = "alias/$type-$newname-key.db";
						}
					} elsif (lc($param) eq 'sslclientauth') {
						$olddsehash{'nssslclientauth'} = $value;
					} else {
						$olddsehash{lc($param)} = $value;
					}
				} elsif (($param eq 'passwdhash') &&
						 ((! $value) || ($value eq ""))) {
					# 3.X used "" as an alias for "clear"
					$oldhash{lc($param)} = 'clear';
				} else {
					$oldhash{lc($param)} = $value;
				}
			}
		}
	}
	close(OLDSRC);

	$oldhash{'errorlog-logging-enabled'} = "off"
		if (! $oldhash{'errorlog-logging-enabled'});
	$oldhash{'accesslog-logging-enabled'} = "off"
		if (! $oldhash{'accesslog-logging-enabled'});
	$oldhash{'auditlog-logging-enabled'} = "off"
		if (! $oldhash{'auditlog-logging-enabled'});

 	# read the old ldbm conf file into a hash table; note that there may not be
	# one, so don't complain
	open( OLDSRC, $oldldbmsrc );
    LINE2: while ( <OLDSRC> ) {
		if (/^\s*#/) { # skip comments
			next LINE2;
		}
		if (/^\s*$/) { # skip blank lines
			next LINE2;
		}
		if (/^index/i) {
			chomp($_);
			if (! &isAStandardIndex($_)) {
				push @newIndex, $_;
			}
			next LINE2;
		}
		if (/^plugin/i) {
			chomp($_);
			if (! &isAStandardPlugin($_)) {
				push @badLdbmPlugins, $_;
			}
			next LINE2;
		}
		if (/^include\s+/i) {
			chomp($inc = $');
			$inc =~ s/\"//g;
			# strip " characters
			if (! &isAStandardInclude($inc)) {
				push @newLdbmInclude, $inc;
			}
			next LINE2;
		}
		if (/^dbcachesize\s+[\"]?(.*?)[\"]?\s*$/i) {
			# strip leading and trailing "
			$param = 'dbcachesize';
			$value = $1;
			if ($value < $MIN_DBCACHESIZE) {
				$value = $MIN_DBCACHESIZE;
			}

			$oldldbmhash{lc($param)} = $value;
			next LINE2;
		}

		if (/^\s*(\S+)\s+[\"]?(.*?)[\"]?\s*$/) {
			# strip leading and trailing "
			$param = $1;
			$value = $2;
			if ($oldParametersToSkip{lc($param)}) {
				next LINE2;
			} elsif (lc($param) eq 'suffix') {
				if (lc($value) cmp 'cn=schema') {
					$oldsuffix{lc($value)} = $value;
				}
			} else {
				$oldldbmhash{lc($param)} = $value;
			}
		}
	}
	close(OLDSRC);
	
 	# read the old dse.ldif file into a hash table; note that there may not be
	# one, so don't complain
	open(OLDSRC, $olddseldif);
	while ( <OLDSRC> ) {
		chomp($_);
		if ( /^passwordchange:\s*/i ) {
			if ($' eq "must") {
				$oldhash{'pw_change'} = "on";
				$oldhash{'pw_must_change'} = "on";
			} elsif ($' eq "may") {
				$oldhash{'pw_change'} = "on";
				$oldhash{'pw_must_change'} = "off";
			} else {
				$oldhash{'pw_change'} = "off";
				$oldhash{'pw_must_change'} = "off";
			}
		} elsif ( /^passwordchecksyntax:\s*/i ) {
			if ($' > 0) {
				$oldhash{'pw_syntax'} = "on";
			} else {
				$oldhash{'pw_syntax'} = "off";
			}
		} elsif ( /^passwordminlength:\s*/i ) {
			$oldhash{'pw_minlength'} = $';
		} elsif ( /^passwordexp:\s*/i ) {
			if ($' > 0) {
				$oldhash{'pw_exp'} = "on";
			} else {
				$oldhash{'pw_exp'} = "off";
			}
		} elsif ( /^passwordmaxage:\s*/i ) {
			$oldhash{'pw_maxage'} = $';
		} elsif ( /^passwordwarning:\s*/i ) {
			$oldhash{'pw_warning'} = $';
		} elsif ( /^passwordkeephistory:\s*/i ) {
			if ($' > 0) {
				$oldhash{'pw_history'} = "on";
			} else {
				$oldhash{'pw_history'} = "off";
			}
		} elsif ( /^passwordinhistory:\s*/i ) {
			$oldhash{'pw_inhistory'} = $';
		} elsif ( /^passwordlockoutduration:\s*/i ) {
			$oldhash{'pw_lockduration'} = $';
		} elsif ( /^passwordlockout:\s*/i ) {
			if ($' > 0) {
				$oldhash{'pw_lockout'} = "on";
			} else {
				$oldhash{'pw_lockout'} = "off";
			}
		} elsif ( /^passwordmaxfailure:\s*/i ) {
			$oldhash{'pw_maxfailure'} = $';
		} elsif ( /^passwordunlock:\s*/i ) {
			if ($' > 0) {
				$oldhash{'pw_unlock'} = "on";
			} else {
				$oldhash{'pw_unlock'} = "off";
			}
		} elsif ( /^passwordresetduration:\s*/i ) {
			$oldhash{'pw_resetfailurecount'} = $';
		}
	}
	close(OLDSRC);

    open(NEWSRC, $newsrc ) || die "Can't open $newsrc: $!: ";
    open(NEWDEST, ">$newsrc.tmp" ) || die "Can't create $newsrc.tmp: $!: ";
    while ( <NEWSRC> ) {
		# make sure the dynamicconf parameter is the last one in the file
		if (/^dynamicconf/i) {
			# print the parameters which exist in the old file but do not
			# exist in the new file; these are the parameters we have not
			# deleted from oldhash
			print NEWDEST "#These additional parameters have been migrated\n";
			foreach $param (sort keys %oldhash) {
				if (lc($param) eq 'passwdhash') {
					$pwhash = $oldhash{lc($param)};
					# if the old value was not set, don't set the new value either
					# just have the server use the default value
					if ($pwhash && $pwhash ne "" && $pwhash ne '""') {
						print NEWDEST 'pw_storagescheme', "\t", $pwhash, "\n";
					}
				} elsif (lc($param) eq 'ntsynchusessl') {
					print NEWDEST 'NTSynchUseSSL', "\t", $oldhash{lc($param)}, "\n";
				} else {
					print NEWDEST $param, "\t", "\"$oldhash{lc($param)}\"",
						"\n";
				}
			}
			print NEWDEST "#End of additional migrated parameters\n\n";
			# use the temp one for now until we have the real one in place, then
			# we will change this back
			print NEWDEST "dynamicconf\t\"$newldbmsrc.tmp\"\n";
		} elsif (/^\s*#/) {
			print NEWDEST $_;
		} elsif (/^include/ && @newInclude) {
			my $newConfDir = $serverHome . '/' . 'config' . '/';
			$newConfDir =~ s#\\#/#g if ($isNT);
			print NEWDEST "# These non standard includes were migrated:\n";
			print "These non standard includes were migrated:\n";
			while (@newInclude) {
				my $oldPath = shift @newInclude;
				# oldPath is a local path; we need a network path here because
				# we will be copying the file
				$oldPath =~ s/^$realOldDir/$oldDir/ig;
				my $base = &basename($oldPath);
				my $newone = $newConfDir . $base;
				# convert to new path
				print NEWDEST "include	", $quote, $newone, $quote, "\n";
				print $newone, "\n";
				# now, change path separators back to the correct ones for
				# the os
				$oldPath =~ s#/#\\#g if ($isNT);
				$newone =~ s#/#\\#g if ($isNT);
				&copyAndEditTextFile($oldPath, $newone, \&fixBinaryAttr);
			}
			print NEWDEST "# end of migrated includes\n";
			print "Be sure to check the new slapd.conf file to make sure the order\n";
			print "is correct and there are no conflicts with new config files,\n";
			print "object classes, attributes, etc.\n";
			print NEWDEST $_;
		} elsif (/^\s*(\S+)\s+[\"]?(.*?)[\"]?\s*$/) {
			$param = $1;
			$value = $2;
			# see if the parameter is set in the old config file
			if ($oldhash{lc($param)}) {
				# only set the parameter if the old value is different than
				# the new value
				if ($value cmp $oldhash{lc($param)}) {
					print NEWDEST "#This parameter was migrated: the original value was $value\n";
					print NEWDEST $param, "\t", "\"$oldhash{lc($param)}\"", "\n";
				} else {
					print NEWDEST $_;
				}
				delete $oldhash{lc($param)};
			} else {
				# just print the parameter
				print NEWDEST $_;
			}
		} else {
			print NEWDEST $_;
		}
	}
	close (NEWSRC);

	# print the bad plugins, commented out, at the end of the file
	if (@badPlugins) {
		print NEWDEST "#The following non standard plugins were detected:\n";
		print "The following non standard plugins were detected:\n";
		foreach (@badPlugins) {
			print NEWDEST "#", $_, "\n";
			print $_, "\n";
		}
		print NEWDEST "#These plugins will probably need to be recompiled for this release\n";
		print "These plugins will probably need to be recompiled for this release\n";
		print NEWDEST "#of directory server, or at the very least, reconfigured.\n";
		print "of directory server, or at the very least, reconfigured.\n";
	}

    close( NEWDEST );

    open(NEWSRC, $newldbmsrc ) || die "Can't open $newldbmsrc: $!: ";
    open(NEWDEST, ">$newldbmsrc.tmp" ) || die "Can't create $newldbmsrc.tmp: $!: ";
    while ( <NEWSRC> ) {
		if (/^\s*#/) {
			print NEWDEST $_;
		} elsif (/^include/ && @newLdbmInclude) {
			my $newConfDir = $serverHome . '/' . 'config' . '/';
			$newConfDir =~ s#\\#/#g if ($isNT);
			print NEWDEST "# These non standard ldbm includes were migrated:\n";
			print "These non standard includes were migrated:\n";
			while (@newLdbmInclude) {
				my $oldPath = shift @newInclude;
				# oldPath is a local path; we need a network path here because
				# we will be copying the file
				$oldPath =~ s/^$realOldDir/$oldDir/ig;
				my $base = &basename($oldPath);
				my $newone = $newConfDir . $base;
				# convert to new path
				print NEWDEST "include	", $quote, $newone, $quote, "\n";
				print $newone, "\n";
				# now, change path separators back to the correct ones for
				# the os
				$oldPath =~ s#/#\\#g if ($isNT);
				$newone =~ s#/#\\#g if ($isNT);
				&copyBinFile($oldPath, $newone);
			}
			print NEWDEST "# end of migrated includes\n";
			print "Be sure to check the new slapd.ldbm.conf file to make sure the order\n";
			print "is correct and there are no conflicts with new config files,\n";
			print "object classes, attributes, etc.\n";
			print NEWDEST $_;
		} elsif (/^\s*(\S+)\s+[\"]?(.*?)[\"]?\s*$/) {
			# strip " characters
			$param = $1;
			$value = $2;
			if (lc($param) eq 'suffix') {
				if ($oldsuffix{lc($value)}) {
					delete $oldsuffix{lc($value)};
				}
				print NEWDEST $_;
			} elsif ($oldhash{lc($param)}) {
				# only set the parameter if the old value is different than
				# the new value
				if ($value cmp $oldhash{lc($param)}) {
					print NEWDEST "#This parameter was migrated: the original value was $value\n";
					print NEWDEST $param, "\t", "\"$oldhash{lc($param)}\"", "\n";
				} else {
					print NEWDEST $_;
				}
				delete $oldhash{lc($param)};
			} elsif ($oldldbmhash{lc($param)}) {
				# only set the parameter if the old value is different than
				# the new value
				if ($value cmp $oldldbmhash{lc($param)}) {
					print NEWDEST "#This parameter was migrated: the original value was $value\n";
					print NEWDEST $param, "\t", "\"$oldldbmhash{lc($param)}\"", "\n";
				} else {
					print NEWDEST $_;
				}
				delete $oldldbmhash{lc($param)};
			} else {
				# just print the parameter
				print NEWDEST $_;
			}
		} else {
			print NEWDEST $_;
		}
	}
	close (NEWSRC);

	# add the suffixes we didn't already have
	if (%oldsuffix) {
		print NEWDEST "#These suffixes were migrated\n";
		foreach (values %oldsuffix) {
			print NEWDEST 'suffix', "\t", "\"$_\"", "\n";
		}
	}

	# add the user defined indexes
	if (@newIndex) {
		print NEWDEST "#These indexes were migrated\n";
		while (@newIndex) {
			print NEWDEST shift(@newIndex), "\n";
		}
	}

	# print the bad plugins, commented out, at the end of the file
	if (@badLdbmPlugins) {
		print NEWDEST "#The following non standard plugins were detected:\n";
		print "The following non standard ldbm plugins were detected:\n";
		foreach (@badLdbmPlugins) {
			print NEWDEST "#", $_, "\n";
			print $_, "\n";
		}
		print NEWDEST "#These plugins will probably need to be recompiled for this release\n";
		print "These plugins will probably need to be recompiled for this release\n";
		print NEWDEST "#of directory server, or at the very least, reconfigured.\n";
		print "of directory server, or at the very least, reconfigured.\n";
	}

	close( NEWDEST );

	open(NEWSRC, $newdseldif ) || die "Can't open $newdseldif: $!: ";
    open(NEWDEST, ">$newdseldif.tmp" ) || die "Can't create $newdseldif.tmp: $!: ";
	$inEncryptionConfig = 0;
    while ( <NEWSRC> ) {
		if (/^\s*#/) {
			print NEWDEST $_;
		} elsif (/^\s*$/) {
			if ($inEncryptionConfig) { # end of entry
				$inEncryptionConfig = 0;
				# if attributes were present in the old config but not
				# in the new one, add them to the end of the entry
				foreach $key (keys %olddsehash) {
					print NEWDEST $key, ': ', $olddsehash{$key}, "\n";
				}
			}
			print NEWDEST $_;
		} elsif (/cn=encryption\s*,\s*cn=config/) {
			$inEncryptionConfig = 1;
			print NEWDEST $_;
		} elsif (/^\s*(\S+):\s*(.*)$/) {
			$param = $1;
			$value = $2;
			if ($olddsehash{lc($param)}) {
				# only set the parameter if the old value is different than
				# the new value
				if ($value cmp $olddsehash{lc($param)}) {
					print NEWDEST $param, "\t", $olddsehash{lc($param)}, "\n";
				} else {
					print NEWDEST $_;
				}
				delete $olddsehash{lc($param)};
			} else {
				# just print the parameter
				print NEWDEST $_;
			}
		} else {
			print NEWDEST $_;
		}
	}
	close (NEWSRC);
    close( NEWDEST );

	# final step: use the slapd_config program to check the new config file
	my $rc = &mySystemNoDie("$root${PATHSEP}bin${PATHSEP}slapd${PATHSEP}server",
							"$root${PATHSEP}bin${PATHSEP}slapd${PATHSEP}server" .
							"${PATHSEP}slapd_config${exe_suffix}",
							'-c', '-f', "$newsrc.tmp");

	# if the check failed, run slapd_config again in verbose mode to provide
	# more information to the user; this will die and abort processing
	if ($rc) {
		print "The following problems were found with the new configuration:\n";
		&mySystem("$root${PATHSEP}bin${PATHSEP}slapd${PATHSEP}server",
				  "$root${PATHSEP}bin${PATHSEP}slapd${PATHSEP}server" .
				  "${PATHSEP}slapd_config${exe_suffix}",
				  '-f', "$newsrc.tmp");
	}

	# if we got here, the files were good
	# save a copy of the old config files
	&copyBinFile("$newsrc", "$newsrc.save");
	&copyBinFile("$newldbmsrc", "$newldbmsrc.save");

	# replace the temporary dynamicconf directive with the real one
	open(NEWSRC, "$newsrc.tmp") or die "Could not open file $newsrc.tmp: $!";
	open(NEWDEST, ">$newsrc") or die "Could not write file $newsrc: $!";
	while(<NEWSRC>) {
		if (/^dynamicconf/i) {
			print NEWDEST "dynamicconf\t\"$newldbmsrc\"\n";
		} else {
			print NEWDEST;
		}
	}
	close NEWSRC;
	close NEWDEST;
	
	&copyBinFile("$newldbmsrc.tmp", "$newldbmsrc");
	&copyBinFile("$newdseldif.tmp", "$newdseldif");
}

sub copyDir {
    my	$src = shift;
    my	$dest = shift;
	my  $exclude = shift;

    opendir( SRC, $src ) or die "Can't open directory $src: $!: ";
	my $mode;
	my $uid;
	my $gid;
    mkdir ( $dest , 0755 ) if !( -e $dest );
	if ($PRESERVE) {
		$mode = (stat($src))[2];
		($uid, $gid) = (stat(_))[4..5];
		# Make sure files owned by the old user are owned by the
		# new user
		if ($uid == $olduid) {
			$uid = $newuid;
			$gid = $newgid;
		}
		chown $uid, $gid, $dest;
		chmod $mode, $dest;
	}
    local ( @files ) = readdir ( SRC );
    closedir( SRC );
    for ( @files ) {
		if ( $_ eq "." || $_ eq ".." ) {
			next;
		} elsif ( $exclude && /$exclude/ ) {
			next;
		} elsif( -d "$src${PATHSEP}$_")  {
			&copyDir ( "$src${PATHSEP}$_", "$dest${PATHSEP}$_" ); 
		} else {
			&copyBinFile ( "$src${PATHSEP}$_", "$dest${PATHSEP}$_");
		}
    }
}

sub copyBinFile {
    my	$src = shift;
    my	$dest = shift;
    my  $buf = "";
	my  $bufsize = 8192;

    open( SRC, $src ) || die "Can't open $src: $!\n";
	# if we are given a directory destination instead of a file, extract the
	# filename portion of the source to use as the destination filename
	if (-d $dest) {
		$dest = $dest . $PATHSEP . &basename($src);
	}
    open( DEST, ">$dest" ) || die "Can't create $dest: $!\n";
    binmode SRC;
    binmode DEST;
	if ($PRESERVE) {
		$mode = (stat($src))[2];
		($uid, $gid) = (stat(_))[4..5];
		# Make sure files owned by the old user are owned by the
		# new user
		if ($uid == $olduid) {
			$uid = $newuid;
			$gid = $newgid;
		}
		chown $uid, $gid, $dest;
		chmod $mode, $dest;
	}
    while (read(SRC, $buf, $bufsize)) {
		print DEST $buf;
    }
    close( SRC );
    close( DEST );
}

sub isAStandardPlugin {
	my $line = shift;

	chomp($line);
	return $stdPlugins{lc($line)};
}

sub isAStandardIndex {
	my $line = shift;

	chomp($line);
	return $stdIndex{$line};
}

sub isAStandardInclude {
	my $line = shift;

	chomp($line);
	return $stdIncludes{lc($line)};
}

# Do a file copy, but convert path names as the file gets copied
# Don't convert paths that don't point anywere, except for log files
# push non-converted paths to the results list
# If you are xlating paths that contain one another, the long paths must come
# first
sub xlatePath {
    my	$src = shift;
    my  $dest = shift;

    open( SRC, $src ) || die "Can't open $src: $!: ";
    open( DEST, ">$dest" ) || die "Can't create $dest: $!: ";
	if ($PRESERVE) {
		$mode = (stat($src))[2];
		($uid, $gid) = (stat(_))[4..5];
		# Make sure files owned by the old user are owned by the
		# new user
		if ($uid == $olduid) {
			$uid = $newuid;
			$gid = $newgid;
		}
		chown $uid, $gid, $dest;
		chmod $mode, $dest;
	}
    while ( <SRC> ) {
		print DEST &xlatePaths( $_, @_ );
    }
    close( SRC );
    close( DEST );
}

# translate paths in the string
sub xlatePaths {
    my	$line = shift;
    my	@otherParams = @_;
    my	$numXs = shift;
    my	@srcPaths = splice( @_, 0, $numXs );
    my	@destPaths = splice( @_, 0, $numXs );
    my	@allowedEmpty = @_;
    my	@pathLengths = map { length( $_ ) } @srcPaths;
    my	$i;
    my	$pre;
    my	$post;
    my	$allowed;
    my	$path;
    my	$destPath;

	# replace the src paths with the dest paths
	# NOTE: this algorithm will only work if the longest paths
	# are replaced first e.g. strlen(srcPath[N]) > strlen(srcPath[N+1])
	# and none of the destpaths match any of the srcpaths
    for ( $i = 0 ; $i < $numXs ; ++$i ) {
		if ($srcPaths[$i] ne $destPaths[$i]) {
			$line =~ s/$srcPaths[$i]/$destPaths[$i]/g;
		}
	}

    return $line;
}

sub copyBak {
    opendir( OLDBAK, "$oldHome${PATHSEP}bak" ) || 
		die "Can't open directory $oldHome${PATHSEP}bak: $!: ";
    local ( @dirs ) = readdir( OLDBAK );
    closedir ( OLDBAK );
    for ( @dirs ) {
		if ( $_ eq "." || $_ eq ".." ) {
            next;
        } elsif ( -d "$oldHome${PATHSEP}bak${PATHSEP}$_" ) {
			$srcDir = "$oldHome${PATHSEP}bak${PATHSEP}$_";
			$destDir = "$serverHome${PATHSEP}bak${PATHSEP}$_";
			$srcLDIF = "$oldHome${PATHSEP}ldif${PATHSEP}bak.ldif";
			$destLDIF = "$serverHome${PATHSEP}ldif${PATHSEP}bak.ldif";
			mkdir( $destDir , 0755 ) if !( -e $destDir);
			# Converting database
			if ( !$isNT && $newuser ) {
				chown($newuid, $newgid, 
					  "$serverHome${PATHSEP}bak", $destDir);
			}
			&other_db2ldif($srcDir, $srcLDIF);
			if ($needAclUpg) {
				&mySystem("$root${PATHSEP}bin${PATHSEP}slapd${PATHSEP}server",
						  "$root${PATHSEP}bin${PATHSEP}slapd${PATHSEP}server" .
						  "${PATHSEP}aclupg$exe_suffix", '-d', '-i',
						  $srcLDIF, '-o', $destLDIF);
			} else {
				&copyBinFile($srcLDIF, $destLDIF);
			}
			&other_ldif2db($destLDIF, $destDir);
		}
	}
}

sub other_db2ldif {
	my  $srcDbDir = shift;
	my  $ldif = shift;

	if ($oldversion == 1) {
		&db2ldif($srcDbDir, $ldif);
	} else {
		# make a dummy version of the current slapd.conf and tell it that
		# the db directory is really the back up directory so that we can
		# trick ns-slapd db2ldif to do the right thing; Oh how I wish there
		# were a simple ldbmcat utility for 3.X and 4.0 . . .
		&xlatePath("$oldHome${PATHSEP}config${PATHSEP}slapd.conf",
				   "$oldHome${PATHSEP}config${PATHSEP}slapd.conf.bak",
				   3,
				   "$realOldHome",
				   "$oldDbDir",
				   "slapd.dynamic_ldbm.conf",
				   "$oldHome",
				   "$srcDbDir",
				   "slapd.dynamic_ldbm.conf.bak",
				   '/logs/');
		&xlatePath("$oldHome${PATHSEP}config${PATHSEP}slapd.dynamic_ldbm.conf",
				   "$oldHome${PATHSEP}config${PATHSEP}slapd.dynamic_ldbm.conf.bak",
				   2,
				   "$realOldHome",
				   "$oldDbDir",
				   "$oldHome",
				   "$srcDbDir",
				   '/logs/');
		# now do the ldif2db with our munged conf files . . .
		&db2ldif($srcDbDir, $ldif,
				 "$oldHome${PATHSEP}config${PATHSEP}slapd.conf.bak");
		unlink("$oldHome${PATHSEP}config${PATHSEP}slapd.conf.bak");
		unlink("$oldHome${PATHSEP}config${PATHSEP}slapd.dynamic_ldbm.conf.bak");
	}
}

sub other_ldif2db {
	my  $ldif = shift;
	my  $destDbDir = shift;
	my  $confFile = shift;
	my  $directiveToAdd = shift;

	# make a dummy version of the current slapd.conf and slapd.ldbm.conf
	# to point to the database directory we want to populate instead of
	# the standard
    &xlatePath("$serverHome${PATHSEP}config${PATHSEP}slapd.conf",
		"$serverHome${PATHSEP}config${PATHSEP}slapd.conf.bak",
		3,
		"$newDbDir",
		"slapd.ldbm.conf",
		"slapd.dynamic_ldbm.conf",
		"$destDbDir",
		"slapd.ldbm.conf.bak",
		"slapd.ldbm.conf.bak",
		'/logs/');
    &xlatePath("$serverHome${PATHSEP}config${PATHSEP}slapd.ldbm.conf",
		"$serverHome${PATHSEP}config${PATHSEP}slapd.ldbm.conf.bak",
		1,
		"$newDbDir",
		"$destDbDir",
		'/logs/');

	# we may need to add something to a config file e.g. when migrating the change
	# log, we need to add suffix $changeLogSuffix to slapd.ldbm.conf in order to
	# ldif2db it without error
	if ($confFile && $directiveToAdd) {
		open(CONFADD, ">>$serverHome${PATHSEP}config${PATHSEP}${confFile}.bak") or
			die "Could not append to $serverHome${PATHSEP}config${PATHSEP}${confFile}.bak: $!";
		print CONFADD $directiveToAdd, "\n";
		close(CONFADD);
	}

    &mySystem("$root${PATHSEP}bin${PATHSEP}slapd${PATHSEP}server",
			  "$root${PATHSEP}bin${PATHSEP}slapd${PATHSEP}server" .
			  "${PATHSEP}$slapdExecName",
			  "ldif2db", '-C', '-f',
			  "$serverHome${PATHSEP}config${PATHSEP}slapd.conf.bak", '-i',
			  "$ldif");
	unlink("$serverHome${PATHSEP}config${PATHSEP}slapd.conf.bak");
	unlink("$serverHome${PATHSEP}config${PATHSEP}slapd.ldbm.conf.bak");
	unlink($ldif);
}

sub manyLdif2db {
	my @args = ();
	while (@_) {
		push @args, '-i', shift(@_);
	}
    &mySystem("$root${PATHSEP}bin${PATHSEP}slapd${PATHSEP}server",
			  "$root${PATHSEP}bin${PATHSEP}slapd${PATHSEP}server" .
			  "${PATHSEP}$slapdExecName", "ldif2db", '-C', '-f',
			  "$serverHome${PATHSEP}config${PATHSEP}slapd.conf", @args);
}

sub copyLdif {
    opendir (LDIFDIR, "$oldHome${PATHSEP}ldif" );
    local ( @files ) = readdir ( LDIFDIR );
	closedir(LDIFDIR);
    for ( @files ) {
		if ( $_ eq "." || $_ eq ".." || $_ eq "demo.ldif" ) {
            next;
        }

		if ($needAclUpg) {
			&mySystem("$root${PATHSEP}bin${PATHSEP}slapd${PATHSEP}server",
					  "$root${PATHSEP}bin${PATHSEP}slapd${PATHSEP}server${PATHSEP}" .
					  "aclupg$exe_suffix", '-d', '-i',
					  "$oldHome${PATHSEP}ldif${PATHSEP}$_", '-o',
					  "$serverHome${PATHSEP}ldif${PATHSEP}$_");
		} else {
			&copyBinFile("$oldHome${PATHSEP}ldif${PATHSEP}$_",
						 "$serverHome${PATHSEP}ldif${PATHSEP}$_");
		}
    }
}

sub genAcl {
    my $filename = "$root${PATHSEP}httpacl${PATHSEP}generated.$type-$newname.acl";

    open( S, ">$filename" ) || die "Can't create file $filename: $!: ";
    print S "version $newversion.0;\n";
    print S "acl agents;\n";
    print S "authenticate (user, group) {\n";
    print S "    prompt = \"Agent Service\";\n";
    print S "};\n";
    print S "deny absolute (all) (user != all);\n";
    print S "allow absolute (all) (user = all);\n";
    print S "\n";
    print S "acl \"default\";\n";
    print S "allow (read, list, execute,info) user = \"anyone\";\n";
    print S "allow (write, delete) user = \"all\";\n";
    close( S );
}

sub getVersion {
	my $rootDir = shift;
	my $version = 0;
	my $minor = 0;
	my $progDir = "${PATHSEP}bin${PATHSEP}slapd${PATHSEP}server${PATHSEP}";
	my $progDir2 = "${PATHSEP}bin${PATHSEP}slapd${PATHSEP}";
	# get the current directory so we can go back to it
	my $curdir = &getCwd;

	# find the slapd executable
	$prog = $rootDir . $progDir . $slapdExecName;
	if (! -f $prog) {
		$prog = $rootDir . $progDir2 . $slapdExecName;
		if (-f $prog && $isNT) {
			# if slapd is in bin/slapd and we're on NT, just assume version 1;
			# apparently, slapd.exe doesn't like the -v argument . . .
			return ( '1', $minor );
		}
	}

	# read the old version from the old slapd program
	chdir($rootDir . $progDir) or
		die "Could not chdir to $rootDir${progDir}: $!: ";
	open(F, "${quote}${quote}$prog${quote} -v${quote} 2>&1 |") or
		die "Could not run slapd program $prog: $!";
	sleep(1); # allow some data to accumulate in the pipe
#	print "Output from $prog -v:\n";
	while (<F>) {
#		print;
		if (/^Netscape-Directory\/(\d+)\.(\d+)/) {
			$version = $1;
			$minor = $2;
			last;
		}
	}
	$code = close(F);
#	print "$prog returned code=$code status=$?\n";

	# done determining versions; go back to orig directory
	chdir($curdir) or die "Could not chdir to $curdir: $!: ";

	$version == 0 and
		die "Could not determine version of the directory server in $rootDir: ";

	return ( $version, $minor );
}

# this subroutine implements a very stupid version of diff
sub diff {
	my $f1 = shift;
	my $f2 = shift;
	my $retval = "";

	open(F1, "$f1") or die "Could not open file $f1";
	open(F2, "$f2") or close(F1), die "Could not open file $f2";

	while (defined($l1 = <F1>) && defined($l2 = <F2>)) {
		if (!($l1 eq $l2)) {
			# ignore comments
			if (($l1 =~ /^#/) && ($l2 =~ /^#/)) {
				 next;
			}
			# ignore whitespace
			$l1 =~ s/\s//g;
			$l2 =~ s/\s//g;
						  
			if (!($l1 eq $l2)) {
				$retval .= "< ${l1}> $l2";
			}
		}
	}

	close(F1);
	close(F2);

	if ($retval eq "") {
		return undef;
	}

	return $retval;
}

# unfortunately, we can't use the shell script/batch file because it may
# not have been updated if the user changed the database directory
sub db2ldif {
	my  ($srcDbDir, $ldif, $conf) = @_;

	if ($oldversion == 1) {
		my $dir = "$oldDir${PATHSEP}bin${PATHSEP}slapd${PATHSEP}server";
		chdir($dir) or die "Error: could not change directory to $dir: $!";
		my @cmd = ("${quote}$dir${PATHSEP}ldbmcat${exe_suffix}${quote}", '-n',
				   "${quote}$srcDbDir${PATHSEP}id2entry.dbb${quote}");
		open(LDBMCAT, "${quote}@cmd${quote}|") or
			die "Error: could not execute @cmd: $!";
		open(OUTLDIF, "> $ldif") or
			die "Error: could not write to $ldif: $!";
		sleep(1); # allow pipe to fill with data
		$ii = 0; # counter
		while (<LDBMCAT>) {
			print OUTLDIF;
			++$ii;
			if (($ii % 250) == 0) {
				print "  Processed ", $ii, " lines\n";
			}
		}
		close(LDBMCAT);
		close(OUTLDIF);
	} else {
		if (!$conf) {
			$conf = "$oldHome${PATHSEP}config${PATHSEP}slapd.conf";
		}
		my $baseldif = &basename($ldif);
		if ($baseldif eq $ldif) {
			$ldif = "$oldHome${PATHSEP}ldif${PATHSEP}$ldif";
		}
		my $dir = "$oldDir${PATHSEP}bin${PATHSEP}slapd${PATHSEP}server";
		chdir($dir) or
			die "Error: could not change directory to $dir: $!";

		my @cmd =
			( "${quote}$oldDir${PATHSEP}bin${PATHSEP}slapd${PATHSEP}server" .
		      "${PATHSEP}$slapdExecName${quote}", "db2ldif", '-n', '-f',
		      "${quote}$conf${quote}", '-a', "${quote}$ldif${quote}",
			  '-d', '1' );
		open(DB2LDIF, "${quote}@cmd${quote} 2>&1|") or
			die "Error: could not execute @cmd: $!";
		sleep(1); # allow pipe to fill with data
		$ii = 0; # counter
		while (<DB2LDIF>) {
			++$ii;
			if (($ii % 250) == 0) {
				print "  Processing...\n";
			}
		}
		close(DB2LDIF);
	}
	print "  Done.\n";
}

# this subroutine works like sed in that it will create another version
# of the input file with some editing done
# the file should be a text file
sub copyAndEditTextFile {
    my $srcFile = shift;
    my $destFile = shift;
    my $sub = shift;
 
    open(SRCFILE, "$srcFile") or die "Error: could not open file $srcFile: $!";
    open(DESTFILE, ">$destFile") or die "Error: could not write file $destFile:
$!";
 
    while (<SRCFILE>) {
        my $newline = &$sub($_);
		if ($newline cmp $_) {
			print "The line: $_";
			print "Was converted to: $newline";
			print "File: $srcFile\n";
		}
        print DESTFILE $newline;
    }
 
    close(SRCFILE);
    close(DESTFILE);
}