From 3670b3631996207e5f400694e586f7fc183e46d6 Mon Sep 17 00:00:00 2001
From: Chris Ridd <chris.ridd@forgerock.com>
Date: Fri, 10 Aug 2012 09:30:43 +0000
Subject: [PATCH] Fix OPENDJ-562 Country String syntax should validate ISO 3166 codes

---
 opendj-sdk/opends/src/admin/defn/org/opends/server/admin/std/CountryStringAttributeSyntaxConfiguration.xml |   76 ++++++++++++
 opendj-sdk/opends/resource/config/config.ldif                                                              |    3 
 opendj-sdk/opends/src/messages/messages/schema.properties                                                  |    3 
 opendj-sdk/opends/src/admin/messages/CountryStringAttributeSyntaxCfgDefn.properties                        |    7 +
 opendj-sdk/opends/src/server/org/opends/server/schema/CountryStringSyntax.java                             |   77 ++++++++++--
 opendj-sdk/opends/resource/schema/02-config.ldif                                                           |    7 +
 opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/schema/CountryStringSyntaxTest.java |  154 +++++++++++++++++++++++++
 7 files changed, 315 insertions(+), 12 deletions(-)

diff --git a/opendj-sdk/opends/resource/config/config.ldif b/opendj-sdk/opends/resource/config/config.ldif
index 1337096..2c773a0 100644
--- a/opendj-sdk/opends/resource/config/config.ldif
+++ b/opendj-sdk/opends/resource/config/config.ldif
@@ -22,6 +22,7 @@
 #
 #      Copyright 2006-2010 Sun Microsystems, Inc.
 #      Portions Copyright 2010-2012 ForgeRock AS.
+#      Portions Copyright 2012 Manuel Gaupp
 #
 #
 # This file contains the primary Directory Server configuration.  It must not
@@ -2067,9 +2068,11 @@
 dn: cn=Country String,cn=Syntaxes,cn=config
 objectClass: top
 objectClass: ds-cfg-attribute-syntax
+objectClass: ds-cfg-country-string-attribute-syntax
 cn: Country String
 ds-cfg-java-class: org.opends.server.schema.CountryStringSyntax
 ds-cfg-enabled: true
+ds-cfg-strict-format: true
 
 dn: cn=Delivery Method,cn=Syntaxes,cn=config
 objectClass: top
diff --git a/opendj-sdk/opends/resource/schema/02-config.ldif b/opendj-sdk/opends/resource/schema/02-config.ldif
index c9271f4..6a0255a 100644
--- a/opendj-sdk/opends/resource/schema/02-config.ldif
+++ b/opendj-sdk/opends/resource/schema/02-config.ldif
@@ -24,6 +24,7 @@
 #      Copyright 2006-2010 Sun Microsystems, Inc.
 #      Portions Copyright 2010-2012 ForgeRock AS.
 #      Portions Copyright 2011 profiq, s.r.o.
+#      Portions Copyright 2012 Manuel Gaupp
 #
 #
 # This file contains the attribute type and objectclass definitions for use
@@ -5154,3 +5155,9 @@
   STRUCTURAL
   MAY ds-cfg-strict-format
   X-ORIGIN 'OpenDJ Directory Server' )
+objectClasses: ( 1.3.6.1.4.1.36733.2.1.2.12
+  NAME 'ds-cfg-country-string-attribute-syntax'
+  SUP ds-cfg-attribute-syntax
+  STRUCTURAL
+  MAY ds-cfg-strict-format
+  X-ORIGIN 'OpenDJ Directory Server' )
diff --git a/opendj-sdk/opends/src/admin/defn/org/opends/server/admin/std/CountryStringAttributeSyntaxConfiguration.xml b/opendj-sdk/opends/src/admin/defn/org/opends/server/admin/std/CountryStringAttributeSyntaxConfiguration.xml
new file mode 100644
index 0000000..e4a9850
--- /dev/null
+++ b/opendj-sdk/opends/src/admin/defn/org/opends/server/admin/std/CountryStringAttributeSyntaxConfiguration.xml
@@ -0,0 +1,76 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  ! CDDL HEADER START
+  !
+  ! The contents of this file are subject to the terms of the
+  ! Common Development and Distribution License, Version 1.0 only
+  ! (the "License").  You may not use this file except in compliance
+  ! with the License.
+  !
+  ! You can obtain a copy of the license at
+  ! trunk/opends/resource/legal-notices/CDDLv1_0.txt
+  ! or http://forgerock.org/license/CDDLv1.0.html.
+  ! See the License for the specific language governing permissions
+  ! and limitations under the License.
+  !
+  ! When distributing Covered Code, include this CDDL HEADER in each
+  ! file and include the License file at
+  ! trunk/opends/resource/legal-notices/CDDLv1_0.txt.  If applicable,
+  ! add the following below this CDDL HEADER, with the fields enclosed
+  ! by brackets "[]" replaced with your own identifying information:
+  !      Portions Copyright [yyyy] [name of copyright owner]
+  !
+  ! CDDL HEADER END
+  !
+  !
+  !      Copyright 2012 ForgeRock AS.
+  !      Portions Copyright 2012 Manuel Gaupp
+  ! -->
+<adm:managed-object name="country-string-attribute-syntax"
+  plural-name="country-string-attribute-syntaxes"
+  extends="attribute-syntax" package="org.opends.server.admin.std"
+  xmlns:adm="http://www.opends.org/admin"
+  xmlns:ldap="http://www.opends.org/admin-ldap">
+  <adm:synopsis>
+    <adm:user-friendly-plural-name />
+    define an attribute syntax for storing country codes.
+  </adm:synopsis>
+  <adm:profile name="ldap">
+    <ldap:object-class>
+      <ldap:name>ds-cfg-country-string-attribute-syntax</ldap:name>
+      <ldap:superior>ds-cfg-attribute-syntax</ldap:superior>
+    </ldap:object-class>
+  </adm:profile>
+  <adm:property-override name="java-class" advanced="true">
+    <adm:default-behavior>
+      <adm:defined>
+        <adm:value>
+          org.opends.server.schema.CountryStringSyntax
+        </adm:value>
+      </adm:defined>
+    </adm:default-behavior>
+  </adm:property-override>
+  <adm:property name="strict-format" advanced="true">
+    <adm:synopsis>
+      Indicates whether or not country code values are required to
+      strictly comply with the standard definition for this syntax.
+    </adm:synopsis>
+    <adm:description>
+      When set to false, country codes will not be validated and, as
+      a result any string containing 2 characters will be acceptable.
+    </adm:description>
+    <adm:default-behavior>
+      <adm:defined>
+        <adm:value>true</adm:value>
+      </adm:defined>
+    </adm:default-behavior>
+    <adm:syntax>
+      <adm:boolean />
+    </adm:syntax>
+    <adm:profile name="ldap">
+      <ldap:attribute>
+        <ldap:name>ds-cfg-strict-format</ldap:name>
+      </ldap:attribute>
+    </adm:profile>
+  </adm:property>
+</adm:managed-object>
diff --git a/opendj-sdk/opends/src/admin/messages/CountryStringAttributeSyntaxCfgDefn.properties b/opendj-sdk/opends/src/admin/messages/CountryStringAttributeSyntaxCfgDefn.properties
new file mode 100644
index 0000000..f8b6e00
--- /dev/null
+++ b/opendj-sdk/opends/src/admin/messages/CountryStringAttributeSyntaxCfgDefn.properties
@@ -0,0 +1,7 @@
+user-friendly-name=Country String Attribute Syntax
+user-friendly-plural-name=Country String Attribute Syntaxes
+synopsis=Country String Attribute Syntaxes define an attribute syntax for storing country codes.
+property.enabled.synopsis=Indicates whether the Country String Attribute Syntax is enabled.
+property.java-class.synopsis=Specifies the fully-qualified name of the Java class that provides the Country String Attribute Syntax implementation.
+property.strict-format.synopsis=Indicates whether or not country code values are required to strictly comply with the standard definition for this syntax.
+property.strict-format.description=When set to false, country codes will not be validated and, as a result any string containing 2 characters will be acceptable.
diff --git a/opendj-sdk/opends/src/messages/messages/schema.properties b/opendj-sdk/opends/src/messages/messages/schema.properties
index 3cd2c95..55ee97e 100644
--- a/opendj-sdk/opends/src/messages/messages/schema.properties
+++ b/opendj-sdk/opends/src/messages/messages/schema.properties
@@ -22,6 +22,7 @@
 #
 #      Copyright 2006-2010 Sun Microsystems, Inc.
 #      Portions Copyright 2011 ForgeRock AS
+#      Portions Copyright 2012 Manuel Gaupp
 
 
 #
@@ -1073,3 +1074,5 @@
  a valid X.509 Certificate because it contains an invalid version number (%d)
 SEVERE_ERR_SYNTAX_CERTIFICATE_INVALID_DER_332=The provided value is not a valid \
  X.509 Certificate because it contains invalid DER encodings
+MILD_ERR_ATTR_SYNTAX_COUNTRY_NO_VALID_ISO_CODE_333=The provided value "%s" \
+ is not a valid ISO 3166 country code
diff --git a/opendj-sdk/opends/src/server/org/opends/server/schema/CountryStringSyntax.java b/opendj-sdk/opends/src/server/org/opends/server/schema/CountryStringSyntax.java
index 231676a..4f957dd 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/schema/CountryStringSyntax.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/schema/CountryStringSyntax.java
@@ -24,12 +24,15 @@
  *
  *      Copyright 2006-2008 Sun Microsystems, Inc.
  *      Portions Copyright 2012 ForgeRock AS
+ *      Portions Copyright 2012 Manuel Gaupp
  */
 package org.opends.server.schema;
 
 
+import java.util.List;
 
-import org.opends.server.admin.std.server.AttributeSyntaxCfg;
+import org.opends.server.admin.server.ConfigurationChangeListener;
+import org.opends.server.admin.std.server.CountryStringAttributeSyntaxCfg;
 import org.opends.server.api.ApproximateMatchingRule;
 import org.opends.server.api.AttributeSyntax;
 import org.opends.server.api.EqualityMatchingRule;
@@ -38,10 +41,13 @@
 import org.opends.server.config.ConfigException;
 import org.opends.server.core.DirectoryServer;
 import org.opends.server.types.ByteSequence;
+import org.opends.server.types.ConfigChangeResult;
+import org.opends.server.types.ResultCode;
 
 
 import static org.opends.server.loggers.ErrorLogger.*;
 import static org.opends.messages.SchemaMessages.*;
+import org.opends.messages.Message;
 import org.opends.messages.MessageBuilder;
 import static org.opends.server.schema.PrintableString.*;
 import static org.opends.server.schema.SchemaConstants.*;
@@ -55,7 +61,8 @@
  * ways, it will behave like the directory string attribute syntax.
  */
 public class CountryStringSyntax
-       extends AttributeSyntax<AttributeSyntaxCfg>
+       extends AttributeSyntax<CountryStringAttributeSyntaxCfg>
+       implements ConfigurationChangeListener<CountryStringAttributeSyntaxCfg>
 {
   // The default approximate matching rule for this syntax.
   private ApproximateMatchingRule defaultApproximateMatchingRule;
@@ -69,6 +76,9 @@
   // The default substring matching rule for this syntax.
   private SubstringMatchingRule defaultSubstringMatchingRule;
 
+  // The current configuration
+  private volatile CountryStringAttributeSyntaxCfg config;
+
 
 
   /**
@@ -87,7 +97,7 @@
   /**
    * {@inheritDoc}
    */
-  public void initializeSyntax(AttributeSyntaxCfg configuration)
+  public void initializeSyntax(CountryStringAttributeSyntaxCfg configuration)
          throws ConfigException
   {
     defaultApproximateMatchingRule =
@@ -121,11 +131,42 @@
       logError(ERR_ATTR_SYNTAX_UNKNOWN_SUBSTRING_MATCHING_RULE.get(
           SMR_CASE_IGNORE_OID, SYNTAX_COUNTRY_STRING_NAME));
     }
+
+    this.config = configuration;
+    config.addCountryStringChangeListener(this);
   }
 
 
 
   /**
+   * {@inheritDoc}
+   */
+  public boolean isConfigurationChangeAcceptable(
+      CountryStringAttributeSyntaxCfg configuration,
+      List<Message> unacceptableReasons)
+  {
+    // The configuration is always acceptable.
+    return true;
+  }
+
+
+
+  /**
+   * {@inheritDoc}
+   */
+  public ConfigChangeResult applyConfigurationChange(
+      CountryStringAttributeSyntaxCfg configuration)
+  {
+    this.config = configuration;
+    return new ConfigChangeResult(ResultCode.SUCCESS, false);
+  }
+
+
+
+
+
+
+  /**
    * Retrieves the common name for this attribute syntax.
    *
    * @return  The common name for this attribute syntax.
@@ -236,7 +277,7 @@
   public boolean valueIsAcceptable(ByteSequence value,
                                    MessageBuilder invalidReason)
   {
-    String stringValue = toLowerCase(value.toString());
+    String stringValue = value.toString();
     if (stringValue.length() != 2)
     {
       invalidReason.append(
@@ -244,16 +285,28 @@
       return false;
     }
 
-
-    if ((! isPrintableCharacter(stringValue.charAt(0))) ||
-        (! isPrintableCharacter(stringValue.charAt(1))))
+    if (config.isStrictFormat())
     {
-      invalidReason.append(
-              ERR_ATTR_SYNTAX_COUNTRY_STRING_NOT_PRINTABLE.get(stringValue));
-      return false;
+      // Check for a string containing [A-Z][A-Z]
+      if (stringValue.charAt(0) < 'A' || stringValue.charAt(0) > 'Z' ||
+          stringValue.charAt(1) < 'A' || stringValue.charAt(1) > 'Z')
+        {
+          invalidReason.append(ERR_ATTR_SYNTAX_COUNTRY_NO_VALID_ISO_CODE
+                 .get(value.toString()));
+          return false;
+        }
     }
-
-
+    else
+    {
+      // Just validate as string containing 2 printable characters
+      if ((! isPrintableCharacter(stringValue.charAt(0))) ||
+          (! isPrintableCharacter(stringValue.charAt(1))))
+      {
+        invalidReason.append(
+                ERR_ATTR_SYNTAX_COUNTRY_STRING_NOT_PRINTABLE.get(stringValue));
+        return false;
+      }
+    }
     return true;
   }
 
diff --git a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/schema/CountryStringSyntaxTest.java b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/schema/CountryStringSyntaxTest.java
new file mode 100644
index 0000000..fe94792
--- /dev/null
+++ b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/schema/CountryStringSyntaxTest.java
@@ -0,0 +1,154 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License, Version 1.0 only
+ * (the "License").  You may not use this file except in compliance
+ * with the License.
+ *
+ * You can obtain a copy of the license at
+ * trunk/opends/resource/legal-notices/OpenDS.LICENSE
+ * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at
+ * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
+ * add the following below this CDDL HEADER, with the fields enclosed
+ * by brackets "[]" replaced with your own identifying information:
+ *      Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ *
+ *
+ *      Copyright 2006-2008 Sun Microsystems, Inc.
+ *      Portions Copyright 2012 Forgerock AS
+ *      Portions Copyright 2012 Manuel Gaupp
+ */
+package org.opends.server.schema;
+
+import org.opends.server.api.AttributeSyntax;
+import org.opends.server.admin.server.ConfigurationChangeListener;
+import org.opends.server.admin.std.server.AttributeSyntaxCfg;
+import org.opends.server.admin.std.server.CountryStringAttributeSyntaxCfg;
+import org.opends.server.config.ConfigException;
+import org.opends.server.types.DN;
+
+import org.testng.annotations.DataProvider;
+
+/**
+ * Test the CountryStringSyntax.
+ */
+public class CountryStringSyntaxTest extends AttributeSyntaxTest
+{
+
+  /**
+   * {@inheritDoc}
+   */
+  @Override
+  protected AttributeSyntax getRule()
+  {
+    CountryStringSyntax syntax = new CountryStringSyntax();
+    CountryStringAttributeSyntaxCfg cfg = new CountryStringAttributeSyntaxCfg()
+    {
+      public DN dn()
+      {
+        return null;
+      }
+
+
+
+      public void removeChangeListener(ConfigurationChangeListener<AttributeSyntaxCfg> listener)
+      {
+        // Stub.
+      }
+
+
+
+      public boolean isEnabled()
+      {
+        // Stub.
+        return false;
+      }
+
+
+
+      public void addChangeListener(
+          ConfigurationChangeListener<AttributeSyntaxCfg> listener)
+      {
+        // Stub.
+      }
+
+
+
+      public void removeCountryStringChangeListener(
+          ConfigurationChangeListener<CountryStringAttributeSyntaxCfg> listener)
+      {
+        // Stub.
+      }
+
+
+
+      public boolean isStrictFormat()
+      {
+        return true;
+      }
+
+
+
+      public String getJavaClass()
+      {
+        // Stub.
+        return null;
+      }
+
+
+
+      public Class<? extends CountryStringAttributeSyntaxCfg> configurationClass()
+      {
+        // Stub.
+        return null;
+      }
+
+
+
+      public void addCountryStringChangeListener(
+          ConfigurationChangeListener<CountryStringAttributeSyntaxCfg> listener)
+      {
+        // Stub.
+      }
+    };
+
+    try
+    {
+      syntax.initializeSyntax(cfg);
+    }
+    catch (ConfigException e)
+    {
+      // Should never happen.
+      throw new RuntimeException(e);
+    }
+
+    return syntax;
+  }
+
+  /**
+   * {@inheritDoc}
+   */
+  @Override
+  @DataProvider(name="acceptableValues")
+  public Object[][] createAcceptableValues()
+  {
+    return new Object [][] {
+        {"DE", true},
+        {"de", false},
+        {"SX", true},
+        {"12", false},
+        {"UK", true},
+        {"Xf", false},
+        {"ÖÄ", false},
+    };
+  }
+
+}

--
Gitblit v1.10.0