mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

neil_a_wilson
25.04.2006 f6023f5673e09974b247444b41702a532a9da399
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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE
 * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
 * add the following below this CDDL HEADER, with the fields enclosed
 * by brackets "[]" replaced with your own identifying * information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *
 *      Portions Copyright 2006 Sun Microsystems, Inc.
 */
package org.opends.server.tools;
 
 
 
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
 
import org.opends.server.config.ConfigFileHandler;
import org.opends.server.core.DirectoryServer;
import org.opends.server.types.DN;
import org.opends.server.types.ExistingFileBehavior;
import org.opends.server.types.LDIFExportConfig;
import org.opends.server.util.LDIFWriter;
import org.opends.server.util.PasswordReader;
import org.opends.server.util.args.ArgumentException;
import org.opends.server.util.args.ArgumentParser;
import org.opends.server.util.args.BooleanArgument;
import org.opends.server.util.args.FileBasedArgument;
import org.opends.server.util.args.IntegerArgument;
import org.opends.server.util.args.StringArgument;
 
import static org.opends.server.messages.MessageHandler.*;
import static org.opends.server.messages.ToolMessages.*;
import static org.opends.server.util.DynamicConstants.*;
import static org.opends.server.util.StaticUtils.*;
 
 
 
/**
 * This class provides a very simple mechanism for installing the OpenDS
 * Directory Service.  It performs the following tasks:
 * <UL>
 *   <LI>Ask the user what base DN should be used for the data</LI>
 *   <LI>Ask the user whether to create the base entry, or to import LDIF</LI>
 *   <LI>Ask the user for the LDAP port and make sure it's available</LI>
 *   <LI>Ask the user for the default root DN and password</LI>
 *   <LI>Ask the user if they want to start the server when done installing</LI>
 * </UL>
 */
public class InstallDS
{
  /**
   * The fully-qualified name of this class for debugging purposes.
   */
  private static final String CLASS_NAME = "org.opends.server.tools.InstallDS";
 
 
 
  /**
   * The position at which to wrap long lines.
   */
  public static final int MAX_LINE_WIDTH = 79;
 
 
 
  /**
   * Indicates whether we think we're running on a Windows system.
   */
  private static boolean isWindows = false;
 
 
 
  /**
   * The version string for the server.
   */
  private static String versionString = "OpenDS Directory Service";
 
 
 
  /**
   * The name of the program used to launch this installation process.
   */
  private static String programName = "setup";
 
 
 
  /**
   * Invokes the <CODE>installMain</CODE> method with the provided arguments.
   *
   * @param  args  The command-line arguments to use for this program.
   */
  public static void main(String[] args)
  {
    int exitCode = installMain(args);
    if (exitCode != 0)
    {
      System.exit(exitCode);
    }
  }
 
 
 
  /**
   * Prompts the user for the necessary information, installs the OpenDS
   * software in the appropriate location, and gives it the desired
   * configuration.
   *
   * @param  args  The command-line arguments to use for this program.
   *
   * @return  A value of zero if the installation process was successful, or a
   *          nonzero value if a problem occurred.
   */
  public static int installMain(String[] args)
  {
    // Determine whether we think we're running on Windows.
    String osName = System.getProperty("os.name");
    if ((osName != null) && (osName.toLowerCase().indexOf("windows") >= 0))
    {
      isWindows = true;
    }
 
 
    // Construct the product version string and the setup filename.
    versionString = PRODUCT_NAME + " " + MAJOR_VERSION + "." + MINOR_VERSION;
    if ((VERSION_QUALIFIER == null) || (VERSION_QUALIFIER.length() == 0))
    {
      versionString += "." + POINT_VERSION;
    }
    else
    {
      versionString += VERSION_QUALIFIER;
    }
 
    if (isWindows)
    {
      programName = "setup.bat";
    }
    else
    {
      programName = "setup.sh";
    }
 
 
    // Create and initialize the argument parser for this program.
    ArgumentParser argParser = new ArgumentParser(CLASS_NAME, false);
    BooleanArgument   addBaseEntry;
    BooleanArgument   testOnly;
    BooleanArgument   showUsage;
    BooleanArgument   silentInstall;
    BooleanArgument   skipPortCheck;
    FileBasedArgument rootPWFile;
    IntegerArgument   ldapPort;
    StringArgument    baseDN;
    StringArgument    configClass;
    StringArgument    configFile;
    StringArgument    importLDIF;
    StringArgument    progName;
    StringArgument    rootDN;
    StringArgument    rootPWString;
 
    try
    {
      testOnly = new BooleanArgument("test", 't', "testOnly",
                                     MSGID_INSTALLDS_DESCRIPTION_TESTONLY);
      testOnly.setHidden(true);
      argParser.addArgument(testOnly);
 
      progName = new StringArgument("progname", 'P', "programName", false,
                                    false, true, "{programName}", programName,
                                    null, MSGID_INSTALLDS_DESCRIPTION_PROGNAME);
      progName.setHidden(true);
      argParser.addArgument(progName);
 
      configFile = new StringArgument("configfile", 'c', "configFile", false,
                                      false, true, "{configFile}", null, null,
                                      MSGID_INSTALLDS_DESCRIPTION_CONFIG_FILE);
      argParser.addArgument(configFile);
 
      configClass = new StringArgument("configclass", 'C', "configClass", false,
                             false, true, "{configClass}",
                             ConfigFileHandler.class.getName(), null,
                             MSGID_INSTALLDS_DESCRIPTION_CONFIG_CLASS);
      argParser.addArgument(configClass);
 
      silentInstall = new BooleanArgument("silent", 's', "silentInstall",
                                          MSGID_INSTALLDS_DESCRIPTION_SILENT);
      argParser.addArgument(silentInstall);
 
      baseDN = new StringArgument("basedn", 'b', "baseDN", false, true, true,
                                  "{baseDN}", "dc=example,dc=com", null,
                                  MSGID_INSTALLDS_DESCRIPTION_BASEDN);
      argParser.addArgument(baseDN);
 
      addBaseEntry = new BooleanArgument("addbase", 'a', "addBaseEntry",
                                         MSGID_INSTALLDS_DESCRIPTION_ADDBASE);
      argParser.addArgument(addBaseEntry);
 
      importLDIF = new StringArgument("importldif", 'i', "importLDIF", false,
                                      true, true, "{ldifFile}", null, null,
                                      MSGID_INSTALLDS_DESCRIPTION_IMPORTLDIF);
      argParser.addArgument(importLDIF);
 
      ldapPort = new IntegerArgument("ldapport", 'p', "ldapPort", false, false,
                                     true, "{port}", 389, null, true, 1, true,
                                     65535,
                                     MSGID_INSTALLDS_DESCRIPTION_LDAPPORT);
      argParser.addArgument(ldapPort);
 
      skipPortCheck = new BooleanArgument("skipportcheck", 'S', "skipPortCheck",
                                          MSGID_INSTALLDS_DESCRIPTION_SKIPPORT);
      argParser.addArgument(skipPortCheck);
 
      rootDN = new StringArgument("rootdn", 'D', "rootUserDN", false, true,
                                  true, "{rootDN}", "cn=Directory Manager",
                                  null, MSGID_INSTALLDS_DESCRIPTION_ROOTDN);
      argParser.addArgument(rootDN);
 
      rootPWString = new StringArgument("rootpwstring", 'w', "rootUserPassword",
                                        false, false, true, "{password}", null,
                                        null,
                                        MSGID_INSTALLDS_DESCRIPTION_ROOTPW);
      argParser.addArgument(rootPWString);
 
      rootPWFile = new FileBasedArgument("rootpwfile", 'W',
                            "rootUserPasswordFile", false, false, "{filename}",
                            null, null, MSGID_INSTALLDS_DESCRIPTION_ROOTPWFILE);
      argParser.addArgument(rootPWFile);
 
      showUsage = new BooleanArgument("help", 'H', "help",
                                      MSGID_INSTALLDS_DESCRIPTION_HELP);
      argParser.addArgument(showUsage);
      argParser.setUsageArgument(showUsage);
    }
    catch (ArgumentException ae)
    {
      System.err.println(wrapText(ae.getMessage(), MAX_LINE_WIDTH));
      System.err.println(argParser.getUsage());
      return 1;
    }
 
 
    // Parse all of the configuration arguments.
    try
    {
      argParser.parseArguments(args);
    }
    catch (ArgumentException ae)
    {
      System.err.println(wrapText(ae.getMessage(), MAX_LINE_WIDTH));
      System.err.println(argParser.getUsage());
      return 1;
    }
 
 
    // If either the showUsage or testOnly arguments were provided, then we're
    // done.
    if (showUsage.isPresent() || testOnly.isPresent())
    {
      return 0;
    }
 
 
    // Make sure the path to the configuration file was given.
    String configFileName;
    if (configFile.isPresent())
    {
      configFileName = configFile.getValue();
    }
    else
    {
      int    msgID   = MSGID_INSTALLDS_NO_CONFIG_FILE;
      String message = getMessage(msgID, configFile.getLongIdentifier());
      System.err.println(wrapText(message, MAX_LINE_WIDTH));
      return 1;
    }
 
 
    // Get the configuration handler class name.
    String configClassName = configClass.getValue();
 
 
    // If this isn't a silent install, then print the version string.
    if (! silentInstall.isPresent())
    {
      System.out.println(versionString);
      System.out.println();
 
      int    msgID   = MSGID_INSTALLDS_INITIALIZING;
      String message = getMessage(msgID);
      System.out.println(wrapText(message, MAX_LINE_WIDTH));
    }
 
 
    // Perform a base-level initialization that will be required to get
    // minimal functionality like DN parsing to work.
    DirectoryServer directoryServer = DirectoryServer.getInstance();
    directoryServer.bootstrapClient();
 
    try
    {
      directoryServer.initializeJMX();
    }
    catch (Exception e)
    {
      int    msgID   = MSGID_INSTALLDS_CANNOT_INITIALIZE_JMX;
      String message = getMessage(msgID,
                                  String.valueOf(configFile.getValue()),
                                  e.getMessage());
      System.err.println(wrapText(message, MAX_LINE_WIDTH));
      return 1;
    }
 
    try
    {
      directoryServer.initializeConfiguration(configClass.getValue(),
                                              configFile.getValue());
    }
    catch (Exception e)
    {
      int    msgID   = MSGID_INSTALLDS_CANNOT_INITIALIZE_CONFIG;
      String message = getMessage(msgID,
                                  String.valueOf(configFile.getValue()),
                                  e.getMessage());
      System.err.println(wrapText(message, MAX_LINE_WIDTH));
      return 1;
    }
 
    try
    {
      directoryServer.initializeSchema();
    }
    catch (Exception e)
    {
      int    msgID   = MSGID_INSTALLDS_CANNOT_INITIALIZE_SCHEMA;
      String message = getMessage(msgID,
                                  String.valueOf(configFile.getValue()),
                                  e.getMessage());
      System.err.println(wrapText(message, MAX_LINE_WIDTH));
      return 1;
    }
 
 
    // Determine the directory base DN.
    LinkedList<DN> baseDNs;
    if (baseDN.isPresent())
    {
      baseDNs = new LinkedList<DN>();
      for (String s : baseDN.getValues())
      {
        try
        {
          baseDNs.add(DN.decode(s));
        }
        catch (Exception e)
        {
          int    msgID   = MSGID_INSTALLDS_CANNOT_PARSE_DN;
          String message = getMessage(msgID, s, e.getMessage());
          System.err.println(wrapText(message, MAX_LINE_WIDTH));
          return 1;
        }
      }
    }
    else if (silentInstall.isPresent())
    {
      try
      {
        baseDNs = new LinkedList<DN>();
        baseDNs.add(DN.decode(baseDN.getDefaultValue()));
      }
      catch (Exception e)
      {
        int    msgID   = MSGID_INSTALLDS_CANNOT_PARSE_DN;
        String message = getMessage(msgID, baseDN.getDefaultValue(),
                                    e.getMessage());
        System.err.println(wrapText(message, MAX_LINE_WIDTH));
        return 1;
      }
    }
    else
    {
      int    msgID   = MSGID_INSTALLDS_PROMPT_BASEDN;
      String message = getMessage(msgID);
 
      baseDNs = new LinkedList<DN>();
      baseDNs.add(promptForDN(message, baseDN.getDefaultValue()));
    }
 
 
    // Determine whether to import data from LDIF.
    LinkedList<String> ldifFiles;
    if (silentInstall.isPresent())
    {
      if (importLDIF.isPresent())
      {
        ldifFiles = importLDIF.getValues();
      }
      else
      {
        ldifFiles = null;
      }
    }
    else if (importLDIF.isPresent())
    {
      ldifFiles = importLDIF.getValues();
    }
    else if (! baseDN.isPresent())
    {
      int     msgID          = MSGID_INSTALLDS_PROMPT_IMPORT;
      String  message        = getMessage(msgID);
      boolean importFromLDIF = promptForBoolean(message, false);
 
      if (importFromLDIF)
      {
        msgID   = MSGID_INSTALLDS_PROMPT_IMPORT_FILE;
        message = getMessage(msgID);
 
        ldifFiles = new LinkedList<String>();
        ldifFiles.add(promptForString(message, null));
      }
      else
      {
        ldifFiles = null;
      }
    }
    else
    {
      ldifFiles = null;
    }
 
 
 
    // Determine whether to add the base entry.
    boolean addBase;
    if (addBaseEntry.isPresent())
    {
      addBase = true;
 
      if ((ldifFiles != null) && (! ldifFiles.isEmpty()))
      {
        int msgID = MSGID_INSTALLDS_TWO_CONFLICTING_ARGUMENTS;
        String message = getMessage(msgID, addBaseEntry.getLongIdentifier(),
                                    importLDIF.getLongIdentifier());
        System.err.println(wrapText(message, MAX_LINE_WIDTH));
        return 1;
      }
    }
    else if (silentInstall.isPresent() ||
             ((ldifFiles != null) && (! ldifFiles.isEmpty())))
    {
      addBase = false;
    }
    else if (baseDN.isPresent())
    {
      addBase = false;
    }
    else
    {
      int    msgID   = MSGID_INSTALLDS_PROMPT_ADDBASE;
      String message = getMessage(msgID, baseDNs.getFirst().toString());
 
      addBase = promptForBoolean(message, true);
    }
 
 
    // Determine the LDAP port number.
    int ldapPortNumber;
    if (silentInstall.isPresent() || ldapPort.isPresent())
    {
      try
      {
        ldapPortNumber = ldapPort.getIntValue();
      }
      catch (ArgumentException ae)
      {
        System.err.println(wrapText(ae.getMessage(), MAX_LINE_WIDTH));
        return 1;
      }
    }
    else
    {
      while (true)
      {
        int    msgID   = MSGID_INSTALLDS_PROMPT_LDAPPORT;
        String message = getMessage(msgID);
        ldapPortNumber = promptForInteger(message, 389, 1, 65535);
 
        if (! skipPortCheck.isPresent())
        {
          try
          {
            InetSocketAddress socketAddress =
                                   new InetSocketAddress(ldapPortNumber);
            ServerSocket serverSocket = new ServerSocket();
            serverSocket.setReuseAddress(true);
            serverSocket.bind(socketAddress);
            serverSocket.close();
            break;
          }
          catch (Exception e)
          {
            if (ldapPortNumber <= 1024)
            {
              msgID   = MSGID_INSTALLDS_CANNOT_BIND_TO_PRIVILEGED_PORT;
              message = getMessage(msgID, ldapPortNumber, e.getMessage());
              System.err.println(wrapText(message, MAX_LINE_WIDTH));
            }
            else
            {
              msgID   = MSGID_INSTALLDS_CANNOT_BIND_TO_PORT;
              message = getMessage(msgID, ldapPortNumber, e.getMessage());
              System.err.println(wrapText(message, MAX_LINE_WIDTH));
            }
          }
        }
      }
    }
 
 
    // Determine the initial root user DN.
    LinkedList<DN> rootDNs;
    if (rootDN.isPresent())
    {
      rootDNs = new LinkedList<DN>();
      for (String s : rootDN.getValues())
      {
        try
        {
          rootDNs.add(DN.decode(s));
        }
        catch (Exception e)
        {
          int    msgID   = MSGID_INSTALLDS_CANNOT_PARSE_DN;
          String message = getMessage(msgID, s, e.getMessage());
          System.err.println(wrapText(message, MAX_LINE_WIDTH));
          return 1;
        }
      }
    }
    else if (silentInstall.isPresent())
    {
      rootDNs = new LinkedList<DN>();
      try
      {
        rootDNs.add(DN.decode(rootDN.getDefaultValue()));
      }
      catch (Exception e)
      {
        int    msgID   = MSGID_INSTALLDS_CANNOT_PARSE_DN;
        String message = getMessage(msgID, rootDN.getDefaultValue(),
                                    e.getMessage());
        System.err.println(wrapText(message, MAX_LINE_WIDTH));
        return 1;
      }
    }
    else
    {
      int    msgID   = MSGID_INSTALLDS_PROMPT_ROOT_DN;
      String message = getMessage(msgID);
 
      rootDNs = new LinkedList<DN>();
      rootDNs.add(promptForDN(message, rootDN.getDefaultValue()));
    }
 
 
    // Determine the initial root user password.
    String rootPassword;
    if (rootPWString.isPresent())
    {
      rootPassword = rootPWString.getValue();
 
      if (rootPWFile.isPresent())
      {
        int msgID = MSGID_INSTALLDS_TWO_CONFLICTING_ARGUMENTS;
        String message = getMessage(msgID, rootPWString.getLongIdentifier(),
                                    rootPWFile.getLongIdentifier());
        System.err.println(wrapText(message, MAX_LINE_WIDTH));
        return 1;
      }
    }
    else if (rootPWFile.isPresent())
    {
      rootPassword = rootPWFile.getValue();
    }
    else if (silentInstall.isPresent())
    {
      int    msgID   = MSGID_INSTALLDS_NO_ROOT_PASSWORD;
      String message = getMessage(msgID, rootPWString.getLongIdentifier(),
                                  rootPWFile.getLongIdentifier());
      System.err.println(wrapText(message, MAX_LINE_WIDTH));
      return 1;
    }
    else
    {
      int    msgID         = MSGID_INSTALLDS_PROMPT_ROOT_PASSWORD;
      String initialPrompt = getMessage(msgID);
 
      msgID = MSGID_INSTALLDS_PROMPT_CONFIRM_ROOT_PASSWORD;
      String confirmPrompt = getMessage(msgID);
 
      rootPassword =
           new String(promptForPassword(initialPrompt, confirmPrompt));
    }
 
 
    // At this point, we should be able to invoke the ConfigureDS utility to
    // apply the requested configuration.
    ArrayList<String> argList = new ArrayList<String>();
    argList.add("-C");
    argList.add(configClassName);
    argList.add("-c");
    argList.add(configFileName);
    argList.add("-p");
    argList.add(String.valueOf(ldapPortNumber));
 
    for (DN dn : baseDNs)
    {
      argList.add("-b");
      argList.add(dn.toString());
    }
 
    for (DN dn : rootDNs)
    {
      argList.add("-D");
      argList.add(dn.toString());
    }
 
    argList.add("-w");
    argList.add(rootPassword);
 
    String[] configureDSArguments = new String[argList.size()];
    argList.toArray(configureDSArguments);
 
    if (! silentInstall.isPresent())
    {
      System.out.println();
 
      String message = getMessage(MSGID_INSTALLDS_STATUS_CONFIGURING_DS);
      System.out.println(wrapText(message, MAX_LINE_WIDTH));
    }
 
    int returnValue = ConfigureDS.configMain(configureDSArguments);
    if (returnValue != 0)
    {
      return returnValue;
    }
 
 
    // If we should import data or add the base entry, then do so now.
    if (addBase)
    {
      // Create a temporary LDIF file that will hold the entry to add.
      if (! silentInstall.isPresent())
      {
        String message = getMessage(MSGID_INSTALLDS_STATUS_CREATING_BASE_LDIF);
        System.out.println(wrapText(message, MAX_LINE_WIDTH));
      }
 
      try
      {
        File   ldifFile     = File.createTempFile("opends-base-entry", ".ldif");
        String ldifFilePath = ldifFile.getAbsolutePath();
        ldifFile.deleteOnExit();
 
        LDIFExportConfig exportConfig =
             new LDIFExportConfig(ldifFilePath, ExistingFileBehavior.OVERWRITE);
        LDIFWriter writer = new LDIFWriter(exportConfig);
 
        for (DN dn : baseDNs)
        {
          writer.writeEntry(createEntry(dn));
        }
 
        writer.close();
 
        if (ldifFiles == null)
        {
          ldifFiles = new LinkedList<String>();
          ldifFiles.add(ldifFilePath);
        }
      }
      catch (Exception e)
      {
        int    msgID   = MSGID_INSTALLDS_CANNOT_CREATE_BASE_ENTRY_LDIF;
        String message = getMessage(msgID, String.valueOf(e));
 
        System.err.println(wrapText(message, MAX_LINE_WIDTH));
        return 1;
      }
    }
 
    if ((ldifFiles != null) && (! ldifFiles.isEmpty()))
    {
      if (! silentInstall.isPresent())
      {
        String message = getMessage(MSGID_INSTALLDS_STATUS_IMPORTING_LDIF);
        System.out.println(wrapText(message, MAX_LINE_WIDTH));
      }
 
      // Use the ImportLDIF tool to perform the import.
      argList = new ArrayList<String>();
      argList.add("-C");
      argList.add(configClassName);
      argList.add("-f");
      argList.add(configFileName);
      argList.add("-n");
      argList.add("userRoot");
 
      for (String s : ldifFiles)
      {
        argList.add("-l");
        argList.add(s);
      }
 
      if (addBase)
      {
        argList.add("-q");
      }
 
      String[] importLDIFArguments = new String[argList.size()];
      argList.toArray(importLDIFArguments);
 
      returnValue = ImportLDIF.mainImportLDIF(importLDIFArguments);
      if (returnValue != 0)
      {
        return returnValue;
      }
      else
      {
        String message = getMessage(MSGID_INSTALLDS_IMPORT_SUCCESSFUL);
        System.out.println(wrapText(message, MAX_LINE_WIDTH));
      }
    }
 
 
    // If we've gotten here, then everything seems to have gone smoothly.
    if (! silentInstall.isPresent())
    {
      String message = getMessage(MSGID_INSTALLDS_STATUS_SUCCESS);
      System.out.println(wrapText(message, MAX_LINE_WIDTH));
    }
 
    return 0;
  }
 
 
 
  /**
   * Interactively prompts (on standard output) the user to provide a Boolean
   * value.  The answer provided must be one of "true", "t", "yes", "y",
   * "false", "f", "no", or "n", ignoring capitalization.  It will keep
   * prompting until an acceptable value is given.
   *
   * @param  prompt        The prompt to present to the user.
   * @param  defaultValue  The default value to assume if the user presses ENTER
   *                       without typing anything, or <CODE>null</CODE> if
   *                       there should not be a default and the user must
   *                       explicitly provide a value.
   *
   * @return  The <CODE>boolean</CODE> value read from the user input.
   */
  private static boolean promptForBoolean(String prompt, Boolean defaultValue)
  {
    String wrappedPrompt = wrapText(prompt, MAX_LINE_WIDTH);
 
    while (true)
    {
      System.out.println();
      System.out.println(wrappedPrompt);
 
      if (defaultValue == null)
      {
        System.out.print(": ");
      }
      else
      {
        System.out.print("[");
 
        if (defaultValue)
        {
          System.out.print(getMessage(MSGID_INSTALLDS_PROMPT_VALUE_YES));
        }
        else
        {
          System.out.print(getMessage(MSGID_INSTALLDS_PROMPT_VALUE_NO));
        }
 
        System.out.print("]: ");
      }
 
      System.out.flush();
 
      String response = toLowerCase(readLine());
      if (response.equals("true") || response.equals("yes") ||
          response.equals("t") || response.equals("y"))
      {
        return true;
      }
      else if (response.equals("false") || response.equals("no") ||
               response.equals("f") || response.equals("n"))
      {
        return false;
      }
      else if (response.equals(""))
      {
        if (defaultValue == null)
        {
          String message = getMessage(MSGID_INSTALLDS_INVALID_YESNO_RESPONSE);
          System.err.println(wrapText(message, MAX_LINE_WIDTH));
        }
        else
        {
          return defaultValue;
        }
      }
      else
      {
        String message = getMessage(MSGID_INSTALLDS_INVALID_YESNO_RESPONSE);
        System.err.println(wrapText(message, MAX_LINE_WIDTH));
      }
    }
  }
 
 
 
  /**
   * Interactively prompts (on standard output) the user to provide an integer
   * value.  The answer provided must be parseable as an integer, and may be
   * required to be within a given set of bounds.  It will keep prompting until
   * an acceptable value is given.
   *
   * @param  prompt        The prompt to present to the user.
   * @param  defaultValue  The default value to assume if the user presses ENTER
   *                       without typing anything, or <CODE>null</CODE> if
   *                       there should not be a default and the user must
   *                       explicitly provide a value.
   * @param  lowerBound    The lower bound that should be enforced, or
   *                       <CODE>null</CODE> if there is none.
   * @param  upperBound    The upper bound that should be enforced, or
   *                       <CODE>null</CODE> if there is none.
   *
   * @return  The <CODE>int</CODE> value read from the user input.
   */
  private static int promptForInteger(String prompt, Integer defaultValue,
                                      Integer lowerBound, Integer upperBound)
  {
    String wrappedPrompt = wrapText(prompt, MAX_LINE_WIDTH);
 
    while (true)
    {
      System.out.println();
      System.out.println(wrappedPrompt);
 
      if (defaultValue == null)
      {
        System.out.print(": ");
      }
      else
      {
        System.out.print("[");
        System.out.print(defaultValue);
        System.out.print("]: ");
      }
 
      System.out.flush();
 
      String response = readLine();
      if (response.equals(""))
      {
        if (defaultValue == null)
        {
          String message = getMessage(MSGID_INSTALLDS_INVALID_INTEGER_RESPONSE);
          System.err.println(wrapText(message, MAX_LINE_WIDTH));
        }
        else
        {
          return defaultValue;
        }
      }
      else
      {
        try
        {
          int intValue = Integer.parseInt(response);
          if ((lowerBound != null) && (intValue < lowerBound))
          {
            String message =
                        getMessage(MSGID_INSTALLDS_INTEGER_BELOW_LOWER_BOUND,
                                   lowerBound);
            System.err.println(wrapText(message, MAX_LINE_WIDTH));
          }
          else if ((upperBound != null) && (intValue > upperBound))
          {
            String message =
                        getMessage(MSGID_INSTALLDS_INTEGER_ABOVE_UPPER_BOUND,
                                   upperBound);
            System.err.println(wrapText(message, MAX_LINE_WIDTH));
          }
          else
          {
            return intValue;
          }
        }
        catch (NumberFormatException nfe)
        {
          String message = getMessage(MSGID_INSTALLDS_INVALID_INTEGER_RESPONSE);
          System.err.println(wrapText(message, MAX_LINE_WIDTH));
        }
      }
    }
  }
 
 
 
  /**
   * Interactively prompts (on standard output) the user to provide a DN value.
   * Any non-empty string will be allowed if it can be parsed as a valid DN (the
   * empty string will indicate that the default should be used, if there is
   * one).
   *
   * @param  prompt        The prompt to present to the user.
   * @param  defaultValue  The default value to assume if the user presses ENTER
   *                       without typing anything, or <CODE>null</CODE> if
   *                       there should not be a default and the user must
   *                       explicitly provide a value.
   *
   * @return  The DN value read from the user.
   */
  private static DN promptForDN(String prompt, String defaultValue)
  {
    String wrappedPrompt = wrapText(prompt, MAX_LINE_WIDTH);
 
    while (true)
    {
      System.out.println();
      System.out.println(wrappedPrompt);
 
      if (defaultValue == null)
      {
        System.out.print(": ");
      }
      else
      {
        System.out.print("[");
        System.out.print(defaultValue);
        System.out.print("]: ");
      }
 
      System.out.flush();
 
      String response = readLine();
      if (response.equals(""))
      {
        if (defaultValue == null)
        {
          String message = getMessage(MSGID_INSTALLDS_INVALID_DN_RESPONSE);
          System.err.println(wrapText(message, MAX_LINE_WIDTH));
        }
        else
        {
          try
          {
            return DN.decode(defaultValue);
          }
          catch (Exception e)
          {
            String message = getMessage(MSGID_INSTALLDS_INVALID_DN_RESPONSE);
            System.err.println(wrapText(message, MAX_LINE_WIDTH));
          }
        }
      }
      else
      {
        try
        {
          return DN.decode(response);
        }
        catch (Exception e)
        {
          String message = getMessage(MSGID_INSTALLDS_INVALID_DN_RESPONSE);
          System.err.println(wrapText(message, MAX_LINE_WIDTH));
        }
      }
    }
  }
 
 
 
  /**
   * Interactively prompts (on standard output) the user to provide a string
   * value.  Any non-empty string will be allowed (the empty string will
   * indicate that the default should be used, if there is one).
   *
   * @param  prompt        The prompt to present to the user.
   * @param  defaultValue  The default value to assume if the user presses ENTER
   *                       without typing anything, or <CODE>null</CODE> if
   *                       there should not be a default and the user must
   *                       explicitly provide a value.
   *
   * @return  The string value read from the user.
   */
  private static String promptForString(String prompt, String defaultValue)
  {
      System.out.println();
    String wrappedPrompt = wrapText(prompt, MAX_LINE_WIDTH);
 
    while (true)
    {
      System.out.println(wrappedPrompt);
 
      if (defaultValue == null)
      {
        System.out.print(": ");
      }
      else
      {
        System.out.print("[");
        System.out.print(defaultValue);
        System.out.print("]: ");
      }
 
      System.out.flush();
 
      String response = readLine();
      if (response.equals(""))
      {
        if (defaultValue == null)
        {
          String message = getMessage(MSGID_INSTALLDS_INVALID_STRING_RESPONSE);
          System.err.println(wrapText(message, MAX_LINE_WIDTH));
        }
        else
        {
          return defaultValue;
        }
      }
      else
      {
        return response;
      }
    }
  }
 
 
 
  /**
   * Interactively prompts (on standard output) the user to provide a string
   * value.  The response that the user provides will not be echoed, and it must
   * be entered twice for confirmation.  No default value will be allowed, and
   * the string entered must contain at least one character.
   *
   * @param  initialPrompt  The initial prompt to present to the user.
   * @param  reEntryPrompt  The prompt to present to the user when requesting
   *                        that the value be re-entered for confirmation.
   *
   * @return  The string value read from the user.
   */
  private static char[] promptForPassword(String initialPrompt,
                                          String reEntryPrompt)
  {
    String wrappedInitialPrompt = wrapText(initialPrompt, MAX_LINE_WIDTH);
    String wrappedReEntryPrompt = wrapText(reEntryPrompt, MAX_LINE_WIDTH);
 
    while (true)
    {
      System.out.println();
      System.out.print(wrappedInitialPrompt);
      System.out.print(": ");
      System.out.flush();
 
      char[] password = PasswordReader.readPassword();
      if ((password == null) || (password.length == 0))
      {
        String message = getMessage(MSGID_INSTALLDS_INVALID_PASSWORD_RESPONSE);
        System.err.println(wrapText(message, MAX_LINE_WIDTH));
      }
      else
      {
        System.out.print(wrappedReEntryPrompt);
        System.out.print(": ");
        System.out.flush();
        char[] confirmedPassword = PasswordReader.readPassword();
        if ((confirmedPassword == null) ||
            (! Arrays.equals(password, confirmedPassword)))
        {
          String message = getMessage(MSGID_INSTALLDS_PASSWORDS_DONT_MATCH);
          System.err.println(wrapText(message, MAX_LINE_WIDTH));
        }
        else
        {
          return password;
        }
      }
    }
  }
 
 
 
  /**
   * Reads a line of text from standard input.
   *
   * @return  The line of text read from standard input, or <CODE>null</CODE>
   *          if the end of the stream is reached or an error occurs while
   *          attempting to read the response.
   */
  private static String readLine()
  {
    try
    {
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
 
      while (true)
      {
        int b = System.in.read();
        if ((b < 0) || (b == '\n'))
        {
          break;
        }
        else if (b == '\r')
        {
          int b2 = System.in.read();
          if (b2 == '\n')
          {
            break;
          }
          else
          {
            baos.write(b);
            baos.write(b2);
          }
        }
        else
        {
          baos.write(b);
        }
      }
 
      return new String(baos.toByteArray(), "UTF-8");
    }
    catch (Exception e)
    {
      String message = getMessage(MSGID_INSTALLDS_ERROR_READING_FROM_STDIN,
                                  String.valueOf(e));
      System.err.println(wrapText(message, MAX_LINE_WIDTH));
 
      return null;
    }
  }
}