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
010     * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011     * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012     * See the License for the specific language governing permissions
013     * and limitations under the License.
014     *
015     * When distributing Covered Code, include this CDDL HEADER in each
016     * file and include the License file at
017     * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
018     * add the following below this CDDL HEADER, with the fields enclosed
019     * by brackets "[]" replaced with your own identifying information:
020     *      Portions Copyright [yyyy] [name of copyright owner]
021     *
022     * CDDL HEADER END
023     *
024     *
025     *      Copyright 2006-2008 Sun Microsystems, Inc.
026     */
027    package org.opends.server.schema;
028    
029    
030    
031    import java.util.Arrays;
032    
033    import org.opends.server.admin.std.server.EqualityMatchingRuleCfg;
034    import org.opends.server.api.EqualityMatchingRule;
035    import org.opends.server.config.ConfigException;
036    import org.opends.server.core.DirectoryServer;
037    import org.opends.server.protocols.asn1.ASN1OctetString;
038    import org.opends.server.types.ByteString;
039    import org.opends.server.types.DirectoryException;
040    
041    
042    import org.opends.server.types.InitializationException;
043    
044    import org.opends.server.types.DebugLogLevel;
045    import static org.opends.server.loggers.ErrorLogger.*;
046    import static org.opends.server.loggers.debug.DebugLogger.*;
047    import org.opends.server.loggers.debug.DebugTracer;
048    import static org.opends.server.schema.SchemaConstants.*;
049    
050    
051    
052    /**
053     * This class implements the authPasswordExactMatch matching rule defined in RFC
054     * 3112.
055     */
056    public class AuthPasswordExactEqualityMatchingRule
057           extends EqualityMatchingRule
058    {
059      /**
060       * The tracer object for the debug logger.
061       */
062      private static final DebugTracer TRACER = getTracer();
063    
064    
065    
066    
067      /**
068       * Creates a new instance of this authPasswordExactMatch matching rule.
069       */
070      public AuthPasswordExactEqualityMatchingRule()
071      {
072        super();
073      }
074    
075    
076    
077      /**
078       * {@inheritDoc}
079       */
080      public void initializeMatchingRule(EqualityMatchingRuleCfg configuration)
081             throws ConfigException, InitializationException
082      {
083        // No initialization is required.
084      }
085    
086    
087    
088      /**
089       * Retrieves the common name for this matching rule.
090       *
091       * @return  The common name for this matching rule, or <CODE>null</CODE> if
092       * it does not have a name.
093       */
094      public String getName()
095      {
096        return EMR_AUTH_PASSWORD_EXACT_NAME;
097      }
098    
099    
100    
101      /**
102       * Retrieves the OID for this matching rule.
103       *
104       * @return  The OID for this matching rule.
105       */
106      public String getOID()
107      {
108        return EMR_AUTH_PASSWORD_EXACT_OID;
109      }
110    
111    
112    
113      /**
114       * Retrieves the description for this matching rule.
115       *
116       * @return  The description for this matching rule, or <CODE>null</CODE> if
117       *          there is none.
118       */
119      public String getDescription()
120      {
121        // There is no standard description for this matching rule.
122        return EMR_AUTH_PASSWORD_EXACT_DESCRIPTION;
123      }
124    
125    
126    
127      /**
128       * Retrieves the OID of the syntax with which this matching rule is
129       * associated.
130       *
131       * @return  The OID of the syntax with which this matching rule is associated.
132       */
133      public String getSyntaxOID()
134      {
135        return SYNTAX_AUTH_PASSWORD_OID;
136      }
137    
138    
139    
140      /**
141       * Retrieves the normalized form of the provided value, which is best suited
142       * for efficiently performing matching operations on that value.
143       *
144       * @param  value  The value to be normalized.
145       *
146       * @return  The normalized version of the provided value.
147       *
148       * @throws  DirectoryException  If the provided value is invalid according to
149       *                              the associated attribute syntax.
150       */
151      public ByteString normalizeValue(ByteString value)
152             throws DirectoryException
153      {
154        try
155        {
156          StringBuilder[] authPWComponents =
157               AuthPasswordSyntax.decodeAuthPassword(value.stringValue());
158    
159          StringBuilder normalizedValue =
160               new StringBuilder(2 + authPWComponents[0].length() +
161                                 authPWComponents[1].length() +
162                                 authPWComponents[2].length());
163          normalizedValue.append(authPWComponents[0]);
164          normalizedValue.append('$');
165          normalizedValue.append(authPWComponents[1]);
166          normalizedValue.append('$');
167          normalizedValue.append(authPWComponents[2]);
168    
169          return new ASN1OctetString(normalizedValue.toString());
170        }
171        catch (DirectoryException de)
172        {
173          if (debugEnabled())
174          {
175            TRACER.debugCaught(DebugLogLevel.ERROR, de);
176          }
177    
178          switch (DirectoryServer.getSyntaxEnforcementPolicy())
179          {
180            case REJECT:
181              throw de;
182            case WARN:
183              logError(de.getMessageObject());
184              return new ASN1OctetString(value.stringValue());
185            default:
186              return new ASN1OctetString(value.stringValue());
187          }
188        }
189      }
190    
191    
192    
193      /**
194       * Indicates whether the two provided normalized values are equal to each
195       * other.
196       *
197       * @param  value1  The normalized form of the first value to compare.
198       * @param  value2  The normalized form of the second value to compare.
199       *
200       * @return  <CODE>true</CODE> if the provided values are equal, or
201       *          <CODE>false</CODE> if not.
202       */
203      public boolean areEqual(ByteString value1, ByteString value2)
204      {
205        // Since the values are already normalized, we just need to compare the
206        // associated byte arrays.
207        return Arrays.equals(value1.value(), value2.value());
208      }
209    }
210