001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2006-2008 Sun Microsystems, Inc.
025 *      Portions Copyright 2014-2015 ForgeRock AS
026 */
027package org.opends.server.protocols.ldap;
028
029import java.io.IOException;
030
031import org.forgerock.opendj.io.*;
032import org.opends.server.types.AuthenticationType;
033import org.forgerock.opendj.ldap.ByteString;
034
035import static org.opends.server.protocols.ldap.LDAPConstants.*;
036import static org.opends.server.util.ServerConstants.*;
037
038/**
039 * This class defines the structures and methods for an LDAP bind request
040 * protocol op, which is used to authenticate a user to the Directory Server.
041 */
042public class BindRequestProtocolOp extends ProtocolOp
043{
044
045  /** The bind DN for this request. */
046  private ByteString dn;
047
048  /** The SASL credentials for this request. */
049  private ByteString saslCredentials;
050
051  /** The simple authentication password for this request. */
052  private ByteString simplePassword;
053
054  /** The authentication type for this request. */
055  private AuthenticationType authenticationType;
056
057  /** The protocol version for this bind request. */
058  private int protocolVersion;
059
060  /** The SASL mechanism for this request. */
061  private String saslMechanism;
062
063
064
065  /**
066   * Creates a new bind request protocol op to perform simple authentication
067   * with the provided DN and password.
068   *
069   * @param  dn               The DN for this bind request.
070   * @param  protocolVersion  The LDAP protocol version for this bind request.
071   * @param  simplePassword   The password for this bind request.
072   */
073  public BindRequestProtocolOp(ByteString dn, int protocolVersion,
074                               ByteString simplePassword)
075  {
076    this.dn              = dn;
077    this.protocolVersion = protocolVersion;
078    this.simplePassword  = simplePassword;
079
080    authenticationType = AuthenticationType.SIMPLE;
081    saslMechanism      = null;
082    saslCredentials    = null;
083  }
084
085
086
087  /**
088   * Creates a new bind request protocol op to perform SASL authentication with
089   * the provided information.
090   *
091   * @param  dn               The DN for this bind request.
092   * @param  saslMechanism    The SASL mechanism for this bind request.
093   * @param  saslCredentials  The SASL credentials for this bind request.
094   */
095  public BindRequestProtocolOp(ByteString dn, String saslMechanism,
096                               ByteString saslCredentials)
097  {
098    this.dn              = dn;
099    this.saslMechanism   = saslMechanism;
100    this.saslCredentials = saslCredentials;
101
102    authenticationType = AuthenticationType.SASL;
103    protocolVersion    = 3;
104    simplePassword     = null;
105  }
106
107
108
109  /**
110   * Retrieves the DN for this bind request.
111   *
112   * @return  The DN for this bind request.
113   */
114  public ByteString getDN()
115  {
116    return dn;
117  }
118
119
120
121  /**
122   * Retrieves the protocol version for this bind request.
123   *
124   * @return  The protocol version for this bind request.
125   */
126  public int getProtocolVersion()
127  {
128    return protocolVersion;
129  }
130
131
132
133  /**
134   * Retrieves the authentication type for this bind request.
135   *
136   * @return  The authentication type for this bind request.
137   */
138  public AuthenticationType getAuthenticationType()
139  {
140    return authenticationType;
141  }
142
143
144
145  /**
146   * Retrieves the simple authentication password for this bind request.
147   *
148   * @return  The simple authentication password for this bind request, or
149   *          <CODE>null</CODE> if this is a SASL bind request.
150   */
151  public ByteString getSimplePassword()
152  {
153    return simplePassword;
154  }
155
156
157
158  /**
159   * Retrieves the SASL mechanism for this bind request.
160   *
161   * @return  The SASL mechanism for this bind request, or <CODE>null</CODE> if
162   *          this is a simple bind request.
163   */
164  public String getSASLMechanism()
165  {
166    return saslMechanism;
167  }
168
169
170
171  /**
172   * Retrieves the SASL credentials for this bind request.
173   *
174   * @return  The SASL credentials for this bind request, or <CODE>null</CODE>
175   *          if there are none or if this is a simple bind request.
176   */
177  public ByteString getSASLCredentials()
178  {
179    return saslCredentials;
180  }
181
182
183
184
185  /**
186   * Retrieves the BER type for this protocol op.
187   *
188   * @return  The BER type for this protocol op.
189   */
190  public byte getType()
191  {
192    return OP_TYPE_BIND_REQUEST;
193  }
194
195
196
197  /**
198   * Retrieves the name for this protocol op type.
199   *
200   * @return  The name for this protocol op type.
201   */
202  public String getProtocolOpName()
203  {
204    return "Bind Request";
205  }
206
207  /**
208   * Writes this protocol op to an ASN.1 output stream.
209   *
210   * @param stream The ASN.1 output stream to write to.
211   * @throws IOException If a problem occurs while writing to the stream.
212   */
213  public void write(ASN1Writer stream) throws IOException
214  {
215    stream.writeStartSequence(OP_TYPE_BIND_REQUEST);
216    stream.writeInteger(protocolVersion);
217    stream.writeOctetString(dn);
218
219    if(authenticationType == AuthenticationType.SIMPLE)
220    {
221      stream.writeOctetString(TYPE_AUTHENTICATION_SIMPLE, simplePassword);
222    }
223    else
224    {
225      stream.writeStartSequence(TYPE_AUTHENTICATION_SASL);
226      stream.writeOctetString(saslMechanism);
227      if(saslCredentials != null)
228      {
229        stream.writeOctetString(saslCredentials);
230      }
231      stream.writeEndSequence();
232    }
233
234    stream.writeEndSequence();
235  }
236
237
238  /**
239   * Appends a string representation of this LDAP protocol op to the provided
240   * buffer.
241   *
242   * @param  buffer  The buffer to which the string should be appended.
243   */
244  public void toString(StringBuilder buffer)
245  {
246    buffer.append("BindRequest(version=").append(protocolVersion);
247    buffer.append(", dn=");
248    if (dn != null)
249    {
250      buffer.append(dn);
251    }
252
253    if (authenticationType == AuthenticationType.SIMPLE)
254    {
255      buffer.append(", password=").append(simplePassword);
256    }
257    else
258    {
259      buffer.append(", saslMechanism=").append(saslMechanism);
260
261      if (saslCredentials != null)
262      {
263        buffer.append(", saslCredentials=").append(saslCredentials);
264      }
265    }
266
267    buffer.append(")");
268  }
269
270
271
272  /**
273   * Appends a multi-line string representation of this LDAP protocol op to the
274   * provided buffer.
275   *
276   * @param  buffer  The buffer to which the information should be appended.
277   * @param  indent  The number of spaces from the margin that the lines should
278   *                 be indented.
279   */
280  public void toString(StringBuilder buffer, int indent)
281  {
282    StringBuilder indentBuf = new StringBuilder(indent);
283    for (int i=0 ; i < indent; i++)
284    {
285      indentBuf.append(' ');
286    }
287
288    buffer.append(indentBuf).append("Bind Request").append(EOL);
289    buffer.append(indentBuf).append("  Protocol Version:  ").append(protocolVersion).append(EOL);
290
291    buffer.append(indentBuf).append("  DN:  ");
292    if (dn != null)
293    {
294      buffer.append(dn);
295    }
296    buffer.append(EOL);
297
298    if (authenticationType == AuthenticationType.SIMPLE)
299    {
300      buffer.append(indentBuf).append("  Simple Password:  ").append(simplePassword).append(EOL);
301    }
302    else
303    {
304      buffer.append(indentBuf).append("  SASL Mechanism:  ").append(saslMechanism).append(EOL);
305
306      if (saslCredentials != null)
307      {
308        buffer.append(indentBuf).append("  SASL Credentials:").append(EOL);
309        buffer.append(saslCredentials.toHexPlusAsciiString(indent+4));
310      }
311    }
312  }
313}
314