Author: markt
Date: Wed Jan 23 14:45:34 2019
New Revision: 1851928

URL: http://svn.apache.org/viewvc?rev=1851928&view=rev
Log:
Fix https://bz.apache.org/bugzilla/show_bug.cgi?id=63026
Add a new attribute, forceDnHexEscape, to the JNDIRealm that forces escaping in 
the String representation of a distinguished name to use the \nn form. This may 
avoid issues with realms using Active Directory which appears to be more 
tolerant of optional escaping when the \nn form is used.

Added:
    
tomcat/trunk/test/org/apache/catalina/realm/TestJNDIRealmConvertToHexEscape.java
   (with props)
Modified:
    tomcat/trunk/java/org/apache/catalina/realm/JNDIRealm.java
    tomcat/trunk/webapps/docs/changelog.xml
    tomcat/trunk/webapps/docs/config/realm.xml

Modified: tomcat/trunk/java/org/apache/catalina/realm/JNDIRealm.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/realm/JNDIRealm.java?rev=1851928&r1=1851927&r2=1851928&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/realm/JNDIRealm.java (original)
+++ tomcat/trunk/java/org/apache/catalina/realm/JNDIRealm.java Wed Jan 23 
14:45:34 2019
@@ -497,8 +497,19 @@ public class JNDIRealm extends RealmBase
      */
     private String sslProtocol;
 
+    private boolean forceDnHexEscape = false;
+
+
     // ------------------------------------------------------------- Properties
 
+    public boolean getForceDnHexEscape() {
+        return forceDnHexEscape;
+    }
+
+    public void setForceDnHexEscape(boolean forceDnHexEscape) {
+        this.forceDnHexEscape = forceDnHexEscape;
+    }
+
     /**
      * @return the type of authentication to use.
      */
@@ -2754,7 +2765,89 @@ public class JNDIRealm extends RealmBase
                        resultName );
            }
         }
-        return name.toString();
+
+        if (getForceDnHexEscape()) {
+            // Bug 63026
+            return convertToHexEscape(name.toString());
+        } else {
+            return name.toString();
+        }
+    }
+
+
+    protected static String convertToHexEscape(String input) {
+        if (input.indexOf('\\') == -1) {
+            // No escaping present. Return original.
+            return input;
+        }
+
+        // +6 allows for 3 escaped characters by default
+        StringBuilder result = new StringBuilder(input.length() + 6);
+        boolean previousSlash = false;
+        for (int i = 0; i < input.length(); i++) {
+            char c = input.charAt(i);
+
+            if (previousSlash) {
+                switch (c) {
+                    case ' ': {
+                        result.append("\\20");
+                        break;
+                    }
+                    case '\"': {
+                        result.append("\\22");
+                        break;
+                    }
+                    case '#': {
+                        result.append("\\23");
+                        break;
+                    }
+                    case '+': {
+                        result.append("\\2B");
+                        break;
+                    }
+                    case ',': {
+                        result.append("\\2C");
+                        break;
+                    }
+                    case ';': {
+                        result.append("\\3B");
+                        break;
+                    }
+                    case '<': {
+                        result.append("\\3C");
+                        break;
+                    }
+                    case '=': {
+                        result.append("\\3D");
+                        break;
+                    }
+                    case '>': {
+                        result.append("\\3E");
+                        break;
+                    }
+                    case '\\': {
+                        result.append("\\5C");
+                        break;
+                    }
+                    default:
+                        result.append('\\');
+                        result.append(c);
+                }
+                previousSlash = false;
+            } else {
+                if (c == '\\') {
+                    previousSlash = true;
+                } else {
+                    result.append(c);
+                }
+            }
+        }
+
+        if (previousSlash) {
+            result.append('\\');
+        }
+
+        return result.toString();
     }
 
 

Added: 
tomcat/trunk/test/org/apache/catalina/realm/TestJNDIRealmConvertToHexEscape.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/realm/TestJNDIRealmConvertToHexEscape.java?rev=1851928&view=auto
==============================================================================
--- 
tomcat/trunk/test/org/apache/catalina/realm/TestJNDIRealmConvertToHexEscape.java
 (added)
+++ 
tomcat/trunk/test/org/apache/catalina/realm/TestJNDIRealmConvertToHexEscape.java
 Wed Jan 23 14:45:34 2019
@@ -0,0 +1,70 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.catalina.realm;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameter;
+
+@RunWith(Parameterized.class)
+public class TestJNDIRealmConvertToHexEscape {
+
+    @Parameterized.Parameters(name = "{index}: in[{0}], out[{1}]")
+    public static Collection<Object[]> parameters() {
+        List<Object[]> parameterSets = new ArrayList<>();
+
+        parameterSets.add(new String[] { "none", "none" });
+        parameterSets.add(new String[] { "\\", "\\" });
+        parameterSets.add(new String[] { "\\\\", "\\5C" });
+        parameterSets.add(new String[] { "\\5C", "\\5C" });
+        parameterSets.add(new String[] { "\\ ", "\\20" });
+        parameterSets.add(new String[] { "\\20", "\\20" });
+        parameterSets.add(new String[] { "\\ foo", "\\20foo" });
+        parameterSets.add(new String[] { "\\20foo", "\\20foo" });
+        parameterSets.add(new String[] { "\\  foo", "\\20 foo" });
+        parameterSets.add(new String[] { "\\20 foo", "\\20 foo" });
+        parameterSets.add(new String[] { "\\ \\ foo", "\\20\\20foo" });
+        parameterSets.add(new String[] { "\\20\\20foo", "\\20\\20foo" });
+        parameterSets.add(new String[] { "foo\\ ", "foo\\20" });
+        parameterSets.add(new String[] { "foo\\20", "foo\\20" });
+        parameterSets.add(new String[] { "foo \\ ", "foo \\20" });
+        parameterSets.add(new String[] { "foo \\20", "foo \\20" });
+        parameterSets.add(new String[] { "foo\\ \\ ", "foo\\20\\20" });
+        parameterSets.add(new String[] { "foo\\20\\20", "foo\\20\\20" });
+
+        return parameterSets;
+    }
+
+
+    @Parameter(0)
+    public String in;
+    @Parameter(1)
+    public String out;
+
+
+    @Test
+    public void testConvertToHexEscape() throws Exception {
+        String result = JNDIRealm.convertToHexEscape(in);
+        Assert.assertEquals(out, result);
+    }
+}

Propchange: 
tomcat/trunk/test/org/apache/catalina/realm/TestJNDIRealmConvertToHexEscape.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: tomcat/trunk/webapps/docs/changelog.xml
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/webapps/docs/changelog.xml?rev=1851928&r1=1851927&r2=1851928&view=diff
==============================================================================
--- tomcat/trunk/webapps/docs/changelog.xml (original)
+++ tomcat/trunk/webapps/docs/changelog.xml Wed Jan 23 14:45:34 2019
@@ -113,6 +113,14 @@
         <code>trimCredentials</code> on the <code>BasicAuthenticator</code>.
         (markt)
       </add>
+      <add>
+        <bug>63026</bug>: Add a new attribute, <code>forceDnHexEscape</code>, 
to
+        the <code>JNDIRealm</code> that forces escaping in the String
+        representation of a distinguished name to use the <code>\nn</code> 
form.
+        This may avoid issues with realms using Active Directory which appears
+        to be more tolerant of optional escaping when the <code>\nn</code> form
+        is used. (markt)
+      </add>
     </changelog>
   </subsection>
   <subsection name="Coyote">

Modified: tomcat/trunk/webapps/docs/config/realm.xml
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/webapps/docs/config/realm.xml?rev=1851928&r1=1851927&r2=1851928&view=diff
==============================================================================
--- tomcat/trunk/webapps/docs/config/realm.xml (original)
+++ tomcat/trunk/webapps/docs/config/realm.xml Wed Jan 23 14:45:34 2019
@@ -456,6 +456,15 @@
         "finding" and "searching". If not specified, "always" is used.</p>
       </attribute>
 
+      <attribute name="forceDnHexEscape" required="false">
+        <p>A setting of <code>true</code> forces escaping in the String
+        representation of a distinguished name to use the <code>\nn</code> 
form.
+        This may avoid issues with realms using Active Directory which appears
+        to be more tolerant of optional escaping when the <code>\nn</code> form
+        is used. If not specified, the default of <code>false</code> will be
+        used.</p>
+      </attribute>
+
       <attribute name="hostnameVerifierClassName" required="false">
         <p>The name of the class to use for hostname verification when
         using StartTLS for securing the connection to the ldap server.



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to