/*
* 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.util.LinkedList;
import org.opends.server.api.ConfigHandler;
import org.opends.server.config.ConfigEntry;
import org.opends.server.config.DNConfigAttribute;
import org.opends.server.config.IntegerConfigAttribute;
import org.opends.server.config.StringConfigAttribute;
import org.opends.server.core.DirectoryServer;
import org.opends.server.core.LockFileManager;
import org.opends.server.extensions.ConfigFileHandler;
import org.opends.server.extensions.SaltedSHA512PasswordStorageScheme;
import org.opends.server.protocols.ldap.LDAPResultCode;
import org.opends.server.types.DirectoryException;
import org.opends.server.types.DN;
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.config.ConfigConstants.*;
import static org.opends.server.messages.ConfigMessages.*;
import static org.opends.server.messages.MessageHandler.*;
import static org.opends.server.messages.ProtocolMessages.*;
import static org.opends.server.messages.ToolMessages.*;
import static org.opends.server.util.ServerConstants.*;
import static org.opends.server.util.StaticUtils.*;
/**
* This class provides a very basic tool that can be used to configure some of
* the most important settings in the Directory Server. This configuration is
* performed by editing the server's configuration files and therefore the
* Directory Server must be offline. This utility will be used during the
* Directory Server installation process.
*
* The options that this tool can currently set include:
*
*
configMain method
* for processing.
*
* @param args The set of command-line arguments provided to this program.
*/
public static void main(String[] args)
{
int exitCode = configMain(args);
if (exitCode != 0)
{
System.exit(exitCode);
}
}
/**
* Parses the provided command-line arguments and makes the appropriate
* changes to the Directory Server configuration.
*
* @param args The command-line arguments provided to this program.
*
* @return The exit code from the configuration processing. A nonzero value
* indicates that there was some kind of problem during the
* configuration processing.
*/
public static int configMain(String[] args)
{
BooleanArgument showUsage;
FileBasedArgument rootPasswordFile;
IntegerArgument ldapPort;
StringArgument baseDNString;
StringArgument configClass;
StringArgument configFile;
StringArgument rootDNString;
StringArgument rootPassword;
String toolDescription = getMessage(MSGID_CONFIGDS_TOOL_DESCRIPTION);
ArgumentParser argParser = new ArgumentParser(CLASS_NAME, toolDescription,
false);
try
{
configFile = new StringArgument("configfile", 'c', "configFile", true,
false, true, "{configFile}", null, null,
MSGID_CONFIGDS_DESCRIPTION_CONFIG_FILE);
configFile.setHidden(true);
argParser.addArgument(configFile);
configClass = new StringArgument("configclass", 'C', "configClass", false,
false, true, "{configClass}",
ConfigFileHandler.class.getName(), null,
MSGID_CONFIGDS_DESCRIPTION_CONFIG_CLASS);
configClass.setHidden(true);
argParser.addArgument(configClass);
ldapPort = new IntegerArgument("ldapport", 'p', "ldapPort", false, false,
true, "{ldapPort}", 389, null, true, 1,
true, 65535,
MSGID_CONFIGDS_DESCRIPTION_LDAP_PORT);
argParser.addArgument(ldapPort);
baseDNString = new StringArgument("basedn", 'b', "baseDN", false, true,
true, "{baseDN}", "dc=example,dc=com",
null,
MSGID_CONFIGDS_DESCRIPTION_BASE_DN);
argParser.addArgument(baseDNString);
rootDNString = new StringArgument("rootdn", 'D', "rootDN", false, false,
true, "{rootUserDN}",
"cn=Directory Manager", null,
MSGID_CONFIGDS_DESCRIPTION_ROOT_DN);
argParser.addArgument(rootDNString);
rootPassword = new StringArgument("rootpw", 'w', "rootPassword", false,
false, true, "{rootUserPW}", null, null,
MSGID_CONFIGDS_DESCRIPTION_ROOT_PW);
argParser.addArgument(rootPassword);
rootPasswordFile = new FileBasedArgument("rootpwfile", 'W',
"rootPasswordFile", false, false,
"{filename}", null, null,
MSGID_CONFIGDS_DESCRIPTION_ROOT_PW_FILE);
argParser.addArgument(rootPasswordFile);
showUsage = new BooleanArgument("showusage", 'H', "help",
MSGID_CONFIGDS_DESCRIPTION_USAGE);
argParser.addArgument(showUsage);
argParser.setUsageArgument(showUsage);
}
catch (ArgumentException ae)
{
int msgID = MSGID_CONFIGDS_CANNOT_INITIALIZE_ARGS;
String message = getMessage(msgID, ae.getMessage());
System.err.println(wrapText(message, MAX_LINE_WIDTH));
return 1;
}
// Parse the command-line arguments provided to the program.
try
{
argParser.parseArguments(args);
}
catch (ArgumentException ae)
{
int msgID = MSGID_CONFIGDS_ERROR_PARSING_ARGS;
String message = getMessage(msgID, ae.getMessage());
System.err.println(wrapText(message, MAX_LINE_WIDTH));
System.err.println(argParser.getUsage());
return LDAPResultCode.CLIENT_SIDE_PARAM_ERROR;
}
// If we should just display usage information, then print it and exit.
if (showUsage.isPresent())
{
return 0;
}
// Make sure that the user actually tried to configure something.
if (! (baseDNString.isPresent() || ldapPort.isPresent() ||
rootDNString.isPresent()))
{
int msgID = MSGID_CONFIGDS_NO_CONFIG_CHANGES;
String message = getMessage(msgID);
System.err.println(wrapText(message, MAX_LINE_WIDTH));
System.err.println(argParser.getUsage());
return 1;
}
// Initialize the Directory Server configuration handler using the
// information that was provided.
DirectoryServer directoryServer = DirectoryServer.getInstance();
directoryServer.bootstrapClient();
try
{
directoryServer.initializeJMX();
}
catch (Exception e)
{
int msgID = MSGID_CONFIGDS_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_CONFIGDS_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_CONFIGDS_CANNOT_INITIALIZE_SCHEMA;
String message = getMessage(msgID,
String.valueOf(configFile.getValue()),
e.getMessage());
System.err.println(wrapText(message, MAX_LINE_WIDTH));
return 1;
}
// Make sure that we can get an exclusive lock for the Directory Server, so
// that no other operation will be allowed while this is in progress.
String serverLockFileName = LockFileManager.getServerLockFileName();
StringBuilder failureReason = new StringBuilder();
if (! LockFileManager.acquireExclusiveLock(serverLockFileName,
failureReason))
{
int msgID = MSGID_CONFIGDS_CANNOT_ACQUIRE_SERVER_LOCK;
String message = getMessage(msgID, String.valueOf(serverLockFileName),
String.valueOf(failureReason));
System.err.println(wrapText(message, MAX_LINE_WIDTH));
return 1;
}
try
{
// If one or more base DNs were provided, then make sure that they can be
// parsed as valid DNs.
LinkedList