cfba646ddfa9d543f0c40e4c7788c65b4a24b34dPhill Cunnington/*
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Copyright (c) 2008 Sun Microsystems Inc. All Rights Reserved
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The contents of this file are subject to the terms
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * of the Common Development and Distribution License
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * (the License). You may not use this file except in
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * compliance with the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * You can obtain a copy of the License at
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * https://opensso.dev.java.net/public/CDDLv1.0.html or
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * opensso/legal/CDDLv1.0.txt
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * See the License for the specific language governing
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * permission and limitations under the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * When distributing Covered Code, include this CDDL
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Header Notice in each file and include the License file
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * at opensso/legal/CDDLv1.0.txt.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * If applicable, add the following below the CDDL Header,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * with the fields enclosed by brackets [] replaced by
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * your own identifying information:
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * "Portions Copyrighted [year] [name of copyright owner]"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * $Id: BootstrapCreator.java,v 1.14 2009/08/03 23:32:54 veiming Exp $
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
cfba646ddfa9d543f0c40e4c7788c65b4a24b34dPhill Cunnington * Portions Copyrighted 2011-2015 ForgeRock AS.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpackage com.sun.identity.setup;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
cfba646ddfa9d543f0c40e4c7788c65b4a24b34dPhill Cunningtonimport static org.forgerock.openam.utils.IOUtils.writeToFile;
cfba646ddfa9d543f0c40e4c7788c65b4a24b34dPhill Cunnington
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.am.util.SystemProperties;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.services.ldap.DSConfigMgr;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.services.ldap.DSConfigMgrBase;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.services.ldap.IDSConfigMgr;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.services.ldap.LDAPServiceException;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.services.ldap.LDAPUser;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.services.ldap.Server;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.services.ldap.ServerGroup;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.services.ldap.ServerInstance;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.services.util.XMLException;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.sso.SSOException;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.common.configuration.ConfigurationException;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.shared.StringUtils;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.sm.SMSException;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.io.File;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.io.IOException;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.net.URLEncoder;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.Collection;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.Iterator;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * This class is responsible for creating bootstrap file based on the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * information in <code>serverconfig.xml</code>.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpublic class BootstrapCreator {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static BootstrapCreator instance = new BootstrapCreator();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static boolean isUnix =
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster System.getProperty("path.separator").equals(":");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster static final String template =
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "@DS_PROTO@://@DS_HOST@/@INSTANCE_NAME@" +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "?user=@DSAMEUSER_NAME@&pwd=@DSAMEUSER_PWD@" +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "&dsbasedn=@BASE_DN@" +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "&dsmgr=@BIND_DN@" +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "&dspwd=@BIND_PWD@" +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "&ver=1.0";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private BootstrapCreator() {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static BootstrapCreator getInstance() {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return instance;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static void updateBootstrap()
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws ConfigurationException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster DSConfigMgrBase dsCfg = new DSConfigMgrBase();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster dsCfg.parseServiceConfigXML();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster instance.update(dsCfg);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (XMLException e) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new ConfigurationException(e.getMessage());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (SMSException e) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new ConfigurationException(e.getMessage());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (SSOException e) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new ConfigurationException(e.getMessage());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static void createBootstrap()
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws ConfigurationException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster instance.update(DSConfigMgr.getDSConfigMgr());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (LDAPServiceException e) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new ConfigurationException(e.getMessage());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private void update(IDSConfigMgr dsCfg)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws ConfigurationException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String bootstrapString = getBootStrapURL(dsCfg);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String baseDir = SystemProperties.get(SystemProperties.CONFIG_PATH);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String file = baseDir + "/" + BootstrapData.BOOTSTRAP;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster File f = new File(file);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster boolean exist = f.exists();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster boolean writable = exist && f.canWrite();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // make bootstrap writable if it is not
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (exist && !writable) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster f.setWritable(true);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Thread.sleep(3000);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
cfba646ddfa9d543f0c40e4c7788c65b4a24b34dPhill Cunnington writeToFile(file, bootstrapString);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // not exist means that the product is first configured.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // set permission to 400
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!exist) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (isUnix) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Runtime.getRuntime().exec("/bin/chmod 400 " + file);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } else {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // make it not writable if it was previously not writable.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!writable) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster f.setWritable(false);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (InterruptedException e) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new ConfigurationException(e.getMessage());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (IOException e) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new ConfigurationException(e.getMessage());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Returns the bootstrap url.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param dsCfg instance of the <code>IDSConfigMgr</code> containing
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * the connection information to the config store.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @exception ConfigurationException if there is an error and cannot
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * obtain the bootstrap URL. This may be due to connection error.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public String getBootStrapURL(IDSConfigMgr dsCfg)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws ConfigurationException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String bootstrapStr = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ServerGroup sg = dsCfg.getServerGroup("sms");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ServerGroup defaultGroup = dsCfg.getServerGroup("default") ;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ServerInstance svrCfg;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (sg == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sg = defaultGroup;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster svrCfg = dsCfg.getServerInstance(LDAPUser.Type.AUTH_ADMIN);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } else {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster svrCfg = sg.getServerInstance(LDAPUser.Type.AUTH_ADMIN);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ServerInstance userInstance = defaultGroup.getServerInstance(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster LDAPUser.Type.AUTH_ADMIN);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String dsameUserName = userInstance.getAuthID();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String dsameUserPwd = JCECrypt.encode(userInstance.getPasswd());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String connDN = svrCfg.getAuthID();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String connPwd = JCECrypt.encode(svrCfg.getPasswd());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String rootSuffix = svrCfg.getBaseDN();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Collection serverList = sg.getServersList();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster StringBuilder bootstrap = new StringBuilder();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster for (Iterator i = serverList.iterator(); i.hasNext(); ) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Server serverObj = (Server)i.next();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Server.Type connType = serverObj.getConnectionType();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String proto = (connType.equals(Server.Type.CONN_SIMPLE)) ?
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "ldap" : "ldaps";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String url = StringUtils.strReplaceAll(template,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "@DS_PROTO@", proto);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String host = serverObj.getServerName() + ":" +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster serverObj.getPort();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster url = StringUtils.strReplaceAll(url, "@DS_HOST@", host);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster url = StringUtils.strReplaceAll(url, "@INSTANCE_NAME@",
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster URLEncoder.encode(SystemProperties.getServerInstanceName(),
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "UTF-8"));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster url = StringUtils.strReplaceAll(url, "@DSAMEUSER_NAME@",
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster URLEncoder.encode(dsameUserName, "UTF-8"));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster url = StringUtils.strReplaceAll(url, "@DSAMEUSER_PWD@",
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster URLEncoder.encode(dsameUserPwd, "UTF-8"));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster url = StringUtils.strReplaceAll(url, "@BASE_DN@",
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster URLEncoder.encode(rootSuffix, "UTF-8"));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster url = StringUtils.strReplaceAll(url, "@BIND_DN@",
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster URLEncoder.encode(connDN, "UTF-8"));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster url = StringUtils.strReplaceAll(url, "@BIND_PWD@",
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster URLEncoder.encode(connPwd, "UTF-8"));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster bootstrap.append(url).append("\n");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster bootstrapStr = bootstrap.toString();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (IOException e) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new ConfigurationException(e.getMessage());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return bootstrapStr;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster}