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

Matthew Swift
16.40.2016 8a7ff716fed166cd42ec42faaabb7d70e317f884
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
/*
 * The contents of this file are subject to the terms of the Common Development and
 * Distribution License (the License). You may not use this file except in compliance with the
 * License.
 *
 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
 * specific language governing permission and limitations under the License.
 *
 * When distributing Covered Software, include this CDDL Header Notice in each file and include
 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
 * Header, with the fields enclosed by brackets [] replaced by your own identifying
 * information: "Portions Copyright [year] [name of copyright owner]".
 *
 * Copyright 2006-2010 Sun Microsystems, Inc.
 * Portions Copyright 2014-2016 ForgeRock AS.
 */
package org.opends.server.protocols.jmx;
 
import static org.opends.messages.ProtocolMessages.*;
 
import java.util.ArrayList;
 
import javax.management.remote.JMXAuthenticator;
import javax.security.auth.Subject;
 
import org.forgerock.i18n.LocalizableMessage;
import org.forgerock.i18n.slf4j.LocalizedLogger;
import org.forgerock.opendj.ldap.ByteString;
import org.forgerock.opendj.ldap.ResultCode;
import org.opends.messages.CoreMessages;
import org.opends.server.api.plugin.PluginResult;
import org.opends.server.core.BindOperationBasis;
import org.opends.server.core.DirectoryServer;
import org.opends.server.core.PluginConfigManager;
import org.opends.server.protocols.ldap.LDAPResultCode;
import org.opends.server.types.AuthenticationInfo;
import org.opends.server.types.Control;
import org.forgerock.opendj.ldap.DN;
import org.opends.server.types.DisconnectReason;
import org.opends.server.types.LDAPException;
import org.opends.server.types.Privilege;
 
/**
 * A <code>RMIAuthenticator</code> manages authentication for the secure
 * RMI connectors. It receives authentication requests from clients as a
 * SASL/PLAIN challenge and relies on a SASL server plus the local LDAP
 * authentication accept or reject the user being connected.
 */
public class RmiAuthenticator implements JMXAuthenticator
{
  private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
 
    /**
     * Indicate if the we are in the finalized phase.
     *
     * @see JmxConnectionHandler
     */
    private boolean finalizedPhase;
 
  /** The JMX Client connection to be used to perform the bind (auth) call. */
  private JmxConnectionHandler jmxConnectionHandler;
 
  /**
   * Constructs a <code>RmiAuthenticator</code>.
   *
   * @param jmxConnectionHandler
   *        The jmxConnectionHandler associated to this RmiAuthenticator
   */
  public RmiAuthenticator(JmxConnectionHandler jmxConnectionHandler)
  {
    this.jmxConnectionHandler = jmxConnectionHandler;
  }
 
  /**
   * Set that we are in the finalized phase.
   *
   * @param finalizedPhase Set to true, it indicates that we are in
   * the finalized phase that that we other connection should be accepted.
   *
   * @see JmxConnectionHandler
   */
  public synchronized void setFinalizedPhase(boolean finalizedPhase)
  {
    this.finalizedPhase = finalizedPhase;
  }
 
  /**
   * Authenticates a RMI client. The credentials received are composed of
   * a SASL/PLAIN authentication id and a password.
   *
   * @param credentials
   *            the SASL/PLAIN credentials to validate
   * @return a <code>Subject</code> holding the principal(s)
   *         authenticated
   */
  @Override
  public Subject authenticate(Object credentials)
  {
    // If we are in the finalized phase, we should not accept new connection
    if (finalizedPhase
        || credentials == null)
    {
      throw new SecurityException();
    }
    Object c[] = (Object[]) credentials;
    String authcID = (String) c[0];
    String password = (String) c[1];
 
    // The authcID is used at forwarder level to identify the calling client
    if (authcID == null)
    {
      logger.trace("User name is Null");
      throw new SecurityException();
    }
    if (password == null)
    {
      logger.trace("User password is Null ");
      throw new SecurityException();
    }
 
    logger.trace("UserName = %s", authcID);
 
    // Try to see if we have an Ldap Authentication
    // Which should be the case in the current implementation
    JmxClientConnection jmxClientConnection;
    try
    {
      jmxClientConnection = bind(authcID, password);
    }
    catch (Exception e)
    {
      logger.traceException(e);
      SecurityException se = new SecurityException(e.getMessage());
      throw se;
    }
 
    // If we've gotten here, then the authentication was successful.
    // We'll take the connection so invoke the post-connect plugins.
    PluginConfigManager pluginManager = DirectoryServer.getPluginConfigManager();
    PluginResult.PostConnect pluginResult = pluginManager.invokePostConnectPlugins(jmxClientConnection);
    if (!pluginResult.continueProcessing())
    {
      jmxClientConnection.disconnect(pluginResult.getDisconnectReason(),
          pluginResult.sendDisconnectNotification(),
          pluginResult.getErrorMessage());
 
      if (logger.isTraceEnabled())
      {
        logger.trace("Disconnect result from post connect plugins: " +
            "%s: %s ", pluginResult.getDisconnectReason(),
            pluginResult.getErrorMessage());
      }
 
      throw new SecurityException();
    }
 
    // initialize a subject
    Subject s = new Subject();
 
    // Add the Principal. The current implementation doesn't use it
    s.getPrincipals().add(new OpendsJmxPrincipal(authcID));
 
    // add the connection client object
    // this connection client is used at forwarder level to identify the calling client
    s.getPrivateCredentials().add(new Credential(jmxClientConnection));
 
    return s;
  }
 
  /**
   * Process bind operation.
   *
   * @param authcID
   *            The LDAP user.
   * @param password
   *            The Ldap password associated to the user.
   */
  private JmxClientConnection bind(String authcID, String password)
  {
    try
    {
      DN.valueOf(authcID);
    }
    catch (Exception e)
    {
      LDAPException ldapEx = new LDAPException(
          LDAPResultCode.INVALID_CREDENTIALS,
          CoreMessages.INFO_RESULT_INVALID_CREDENTIALS.get());
      throw new SecurityException(ldapEx);
    }
 
    ArrayList<Control> requestControls = new ArrayList<>();
    ByteString bindPW = password != null ? ByteString.valueOfUtf8(password) : null;
 
    AuthenticationInfo authInfo = new AuthenticationInfo();
    JmxClientConnection jmxClientConnection = new JmxClientConnection(
        jmxConnectionHandler, authInfo);
 
    BindOperationBasis bindOp = new BindOperationBasis(jmxClientConnection,
        jmxClientConnection.nextOperationID(),
        jmxClientConnection.nextMessageID(), requestControls,
        jmxConnectionHandler.getRMIConnector().getProtocolVersion(),
        ByteString.valueOfUtf8(authcID), bindPW);
 
    bindOp.run();
    if (bindOp.getResultCode() == ResultCode.SUCCESS)
    {
      logger.trace("User is authenticated");
 
      authInfo = bindOp.getAuthenticationInfo();
      jmxClientConnection.setAuthenticationInfo(authInfo);
 
      // Check JMX_READ privilege.
      if (! jmxClientConnection.hasPrivilege(Privilege.JMX_READ, null))
      {
        LocalizableMessage message = ERR_JMX_INSUFFICIENT_PRIVILEGES.get();
 
        jmxClientConnection.disconnect(DisconnectReason.CONNECTION_REJECTED,
            false, message);
 
        throw new SecurityException(message.toString());
      }
      return jmxClientConnection;
    }
    else
    {
      // Set the initcause.
      LDAPException ldapEx = new LDAPException(
          LDAPResultCode.INVALID_CREDENTIALS,
          CoreMessages.INFO_RESULT_INVALID_CREDENTIALS.get());
      SecurityException se = new SecurityException("return code: " + bindOp.getResultCode());
      se.initCause(ldapEx);
      throw se;
    }
  }
}