001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2008-2010 Sun Microsystems, Inc.
015 * Portions Copyright 2014-2016 ForgeRock AS.
016 */
017package org.opends.server.tasks;
018
019import static org.opends.messages.TaskMessages.*;
020
021import java.net.InetAddress;
022
023import org.forgerock.i18n.LocalizableMessage;
024import org.forgerock.opendj.ldap.DN;
025import org.forgerock.opendj.ldap.ResultCode;
026import org.opends.server.backends.task.Task;
027import org.opends.server.backends.task.TaskState;
028import org.opends.server.core.DirectoryServer;
029import org.opends.server.types.DirectoryException;
030import org.opends.server.types.Operation;
031import org.opends.server.types.Privilege;
032
033/**
034 * This class provides an implementation of a Directory Server task that can be
035 * used to place the server in lockdown mode.
036 */
037public class EnterLockdownModeTask
038       extends Task
039{
040  @Override
041  public LocalizableMessage getDisplayName() {
042    return INFO_TASK_ENTER_LOCKDOWN_MODE_NAME.get();
043  }
044
045  @Override
046  public void initializeTask()
047         throws DirectoryException
048  {
049    // If the client connection is available, then make sure it is authorized
050    // as a root user.
051    Operation operation = getOperation();
052    if (operation != null)
053    {
054      DN authzDN = operation.getAuthorizationDN();
055      if (authzDN == null || !operation.getClientConnection().hasPrivilege(
056          Privilege.SERVER_LOCKDOWN, operation))
057      {
058        LocalizableMessage message = ERR_TASK_ENTERLOCKDOWN_NOT_ROOT.get();
059        throw new DirectoryException(ResultCode.UNWILLING_TO_PERFORM, message);
060      }
061
062      InetAddress clientAddress = operation.getClientConnection().getRemoteAddress();
063      if (clientAddress != null && !clientAddress.isLoopbackAddress())
064      {
065        LocalizableMessage message = ERR_TASK_ENTERLOCKDOWN_NOT_LOOPBACK.get();
066        throw new DirectoryException(ResultCode.UNWILLING_TO_PERFORM, message);
067      }
068    }
069  }
070
071  @Override
072  protected TaskState runTask()
073  {
074    DirectoryServer.setLockdownMode(true);
075    return TaskState.COMPLETED_SUCCESSFULLY;
076  }
077}