Bug 1308541 - ldapjdk URL parsing mishandles ldap:///
authorRich Megginson <richm@stanfordalumni.org>
Fri, 07 Oct 2016 15:31:23 -0600
changeset 47 88f411aabcde
parent 46 f9d7552c8188
child 48 a34932a9856f
push id32
push userrichm@stanfordalumni.org
push dateFri, 07 Oct 2016 21:31:28 +0000
bugs1308541
Bug 1308541 - ldapjdk URL parsing mishandles ldap:/// https://bugzilla.mozilla.org/show_bug.cgi?id=1308541 Fix handling of ldap:/// URLs. Committed by: Mark Reynolds <mreynolds@redhat.com>
java-sdk/ldapjdk/netscape/ldap/LDAPUrl.java
--- a/java-sdk/ldapjdk/netscape/ldap/LDAPUrl.java
+++ b/java-sdk/ldapjdk/netscape/ldap/LDAPUrl.java
@@ -131,20 +131,20 @@ public class LDAPUrl implements java.io.
     }
 
     /**
      * Parse URL as defined in RFC 1959. Beyond the RFC, the secure ldap
      * (ldaps) is also supported.
      */
     private void parseUrl(String url) throws MalformedURLException {
         StringTokenizer urlParser = new StringTokenizer (url, ":/?[]", true);
-        StringTokenizer markParser = new StringTokenizer (url, ":/?[]", true);
         String currentToken;
         String str = null;
         boolean usingIPv6 = false;
+        boolean usingDefaultHostPort = false;
 
         try {
             currentToken = urlParser.nextToken();
             if (currentToken.equalsIgnoreCase ("LDAPS")) {
                 m_secure = true;
             }
             else if (!currentToken.equalsIgnoreCase ("LDAP")) {
                 throw new MalformedURLException ();
@@ -170,16 +170,17 @@ public class LDAPUrl implements java.io.
         catch (NoSuchElementException e) {
             throw new MalformedURLException ();
         }
             
         // host-port
         if (currentToken.equals ("/")) {
             m_hostName = null;
             m_portNumber = m_secure ? DEFAULT_SECURE_PORT : LDAPv2.DEFAULT_PORT;
+            usingDefaultHostPort = true;
         } else if (currentToken.equals (":")) {
                 // port number without host name is not allowed
                throw new MalformedURLException ("No hostname");
         } else if (currentToken.equals ("?")) {
             throw new MalformedURLException ("No host[:port]");
         } else if (usingIPv6){
             StringBuilder sb = new StringBuilder();
             while (urlParser.hasMoreElements()) {
@@ -189,47 +190,48 @@ public class LDAPUrl implements java.io.
                 }
                 sb.append(currentToken);
             }
             m_hostName = sb.toString();
         } else {
             m_hostName = currentToken;
         }
         
-        // Set the port
-        if (urlParser.countTokens() == 0) {
-            m_portNumber = m_secure ? DEFAULT_SECURE_PORT : LDAPv2.DEFAULT_PORT;
-            return;
-        }
-        currentToken = urlParser.nextToken (); // either ":" or "/"
-
-        if (currentToken.equals (":")) {
-            try {
-                m_portNumber = Integer.parseInt (urlParser.nextToken());
-            } catch (NumberFormatException nf) {
-                throw new MalformedURLException ("Port not a number");
-            } catch (NoSuchElementException ex) {
-                throw new MalformedURLException ("No port number");
-            }
-                
+        if (usingDefaultHostPort == false) {
+            // Set the port
             if (urlParser.countTokens() == 0) {
+                m_portNumber = m_secure ? DEFAULT_SECURE_PORT : LDAPv2.DEFAULT_PORT;
                 return;
             }
-            else if (! urlParser.nextToken().equals("/")) {
+
+            // either ":","/", or the base DN if using "ldap:///"
+            currentToken = urlParser.nextToken ();
+
+            if (currentToken.equals (":")) {
+                try {
+                    m_portNumber = Integer.parseInt (urlParser.nextToken());
+                } catch (NumberFormatException nf) {
+                    throw new MalformedURLException ("Port not a number");
+                } catch (NoSuchElementException ex) {
+                    throw new MalformedURLException ("No port number");
+                }
+                if (urlParser.countTokens() == 0) {
+                    return;
+                }
+                else if (! urlParser.nextToken().equals("/")) {
+                    throw new MalformedURLException ();
+                }
+            } else if (currentToken.equals ("/")) {
+                m_portNumber = m_secure ? DEFAULT_SECURE_PORT : LDAPv2.DEFAULT_PORT;
+            } else {
+                // expecting ":" or "/"
                 throw new MalformedURLException ();
             }
-
-        } else if (currentToken.equals ("/")) {
-            m_portNumber = m_secure ? DEFAULT_SECURE_PORT : LDAPv2.DEFAULT_PORT;
-        } else {
-            // expecting ":" or "/"
-            throw new MalformedURLException ();
         }
 
-
         // DN
         if (!urlParser.hasMoreTokens ()) {
             return;
         }
         m_DN = decode(readNextConstruct(urlParser));
         if (m_DN.equals("?")) {
             m_DN = "";
         }