changeset 5608:1c56d164dee5

8003948: NTLM/Negotiate authentication problem Reviewed-by: chegar, weijun
author michaelm
date Tue, 27 Nov 2012 16:30:23 -0800
parents 579463b11b0e
children 115d1e436529
files src/share/classes/sun/net/www/MessageHeader.java src/share/classes/sun/net/www/protocol/http/HttpURLConnection.java test/sun/net/www/messageheader/0 test/sun/net/www/messageheader/1 test/sun/net/www/messageheader/2 test/sun/net/www/messageheader/3 test/sun/net/www/messageheader/4 test/sun/net/www/messageheader/5 test/sun/net/www/messageheader/6 test/sun/net/www/messageheader/HTest.java
diffstat 10 files changed, 165 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/sun/net/www/MessageHeader.java	Wed Nov 21 21:10:25 2012 -0800
+++ b/src/share/classes/sun/net/www/MessageHeader.java	Tue Nov 27 16:30:23 2012 -0800
@@ -138,6 +138,43 @@
         return null;
     }
 
+    /**
+     * Removes bare Negotiate and Kerberos headers when an "NTLM ..."
+     * appears. All Performed on headers with key being k.
+     * @return true if there is a change
+     */
+    public boolean filterNTLMResponses(String k) {
+        boolean found = false;
+        for (int i=0; i<nkeys; i++) {
+            if (k.equalsIgnoreCase(keys[i])
+                    && values[i] != null && values[i].length() > 5
+                    && values[i].substring(0, 5).equalsIgnoreCase("NTLM ")) {
+                found = true;
+                break;
+            }
+        }
+        if (found) {
+            int j = 0;
+            for (int i=0; i<nkeys; i++) {
+                if (k.equalsIgnoreCase(keys[i]) && (
+                        "Negotiate".equalsIgnoreCase(values[i]) ||
+                        "Kerberos".equalsIgnoreCase(values[i]))) {
+                    continue;
+                }
+                if (i != j) {
+                    keys[j] = keys[i];
+                    values[j] = values[i];
+                }
+                j++;
+            }
+            if (j != nkeys) {
+                nkeys = j;
+                return true;
+            }
+        }
+        return false;
+    }
+
     class HeaderIterator implements Iterator<String> {
         int index = 0;
         int next = -1;
--- a/src/share/classes/sun/net/www/protocol/http/HttpURLConnection.java	Wed Nov 21 21:10:25 2012 -0800
+++ b/src/share/classes/sun/net/www/protocol/http/HttpURLConnection.java	Tue Nov 27 16:30:23 2012 -0800
@@ -1323,6 +1323,16 @@
                 if (logger.isLoggable(PlatformLogger.FINE)) {
                     logger.fine(responses.toString());
                 }
+
+                boolean b1 = responses.filterNTLMResponses("WWW-Authenticate");
+                boolean b2 = responses.filterNTLMResponses("Proxy-Authenticate");
+                if (b1 || b2) {
+                    if (logger.isLoggable(PlatformLogger.FINE)) {
+                        logger.fine(">>>> Headers are filtered");
+                        logger.fine(responses.toString());
+                    }
+                }
+
                 inputStream = http.getInputStream();
 
                 respCode = getResponseCode();
@@ -1782,6 +1792,13 @@
                     logger.fine(responses.toString());
                 }
 
+                if (responses.filterNTLMResponses("Proxy-Authenticate")) {
+                    if (logger.isLoggable(PlatformLogger.FINE)) {
+                        logger.fine(">>>> Headers are filtered");
+                        logger.fine(responses.toString());
+                    }
+                }
+
                 statusLine = responses.getValue(0);
                 StringTokenizer st = new StringTokenizer(statusLine);
                 st.nextToken();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/sun/net/www/messageheader/0	Tue Nov 27 16:30:23 2012 -0800
@@ -0,0 +1,4 @@
+HTTP/1.1 200 Ok
+Foo: bar
+Bar: foo
+WWW-Authenticate: NTLM sdsds
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/sun/net/www/messageheader/1	Tue Nov 27 16:30:23 2012 -0800
@@ -0,0 +1,4 @@
+HTTP/1.1 200 Ok
+Foo: bar
+Bar: foo
+WWW-Authenticate:
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/sun/net/www/messageheader/2	Tue Nov 27 16:30:23 2012 -0800
@@ -0,0 +1,5 @@
+HTTP/1.1 200 Ok
+Foo: bar
+Bar: foo
+WWW-Authenticate: NTLM sdsds
+WWW-Authenticate: Negotiate
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/sun/net/www/messageheader/3	Tue Nov 27 16:30:23 2012 -0800
@@ -0,0 +1,6 @@
+HTTP/1.1 200 Ok
+Foo: bar
+Bar: foo
+WWW-Authenticate: NTLM sdsds
+WWW-Authenticate: Negotiate
+WWW-Authenticate: Kerberos
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/sun/net/www/messageheader/4	Tue Nov 27 16:30:23 2012 -0800
@@ -0,0 +1,7 @@
+HTTP/1.1 200 Ok
+WWW-Authenticate: Negotiate
+Foo: bar
+Bar: foo
+WWW-Authenticate: NTLM sdsds
+Bar: foo
+WWW-Authenticate: Kerberos
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/sun/net/www/messageheader/5	Tue Nov 27 16:30:23 2012 -0800
@@ -0,0 +1,7 @@
+HTTP/1.1 200 Ok
+WWW-Authenticate: Negotiate
+Foo: bar
+Bar: foo
+WWW-Authenticate: NTLM
+Bar: foo
+WWW-Authenticate: Kerberos
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/sun/net/www/messageheader/6	Tue Nov 27 16:30:23 2012 -0800
@@ -0,0 +1,5 @@
+HTTP/1.1 200 Ok
+Foo: foo
+Bar:
+WWW-Authenticate: NTLM blob
+Bar: foo blob
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/sun/net/www/messageheader/HTest.java	Tue Nov 27 16:30:23 2012 -0800
@@ -0,0 +1,73 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * @test
+ * @bug 8003948
+ * @run main HTest
+ */
+import java.io.*;
+import sun.net.www.MessageHeader;
+
+public class HTest {
+    public static void main (String[] args) throws Exception {
+        String prefix = System.getProperty("test.src");
+        System.out.println ("TEST.SRC = " + prefix);
+        for (int i=0; i<7; i++) {
+            File f = new File(prefix, Integer.toString(i));
+            FileInputStream fis = new FileInputStream(f);
+            MessageHeader h = new MessageHeader(fis);
+            String before = h.toString();
+            before = before.substring(before.indexOf('{'));
+            System.out.println ("Before");
+            System.out.println (before);
+            boolean result = h.filterNTLMResponses("WWW-Authenticate");
+            String after = h.toString();
+            after = after.substring(after.indexOf('{'));
+            System.out.println ("After");
+            System.out.println (after);
+            System.out.println ("Expected");
+            System.out.println (expected[i]);
+            if (!expected[i].equals(after)) {
+                throw new RuntimeException(Integer.toString(i) + " expected != after");
+            }
+            if (result != expectedResult[i]) {
+                throw new RuntimeException(Integer.toString(i) + " result != expectedResult");
+            }
+        }
+    }
+
+    static String expected[] = {
+        "{null: HTTP/1.1 200 Ok}{Foo: bar}{Bar: foo}{WWW-Authenticate: NTLM sdsds}",
+        "{null: HTTP/1.1 200 Ok}{Foo: bar}{Bar: foo}{WWW-Authenticate: }",
+        "{null: HTTP/1.1 200 Ok}{Foo: bar}{Bar: foo}{WWW-Authenticate: NTLM sdsds}",
+        "{null: HTTP/1.1 200 Ok}{Foo: bar}{Bar: foo}{WWW-Authenticate: NTLM sdsds}",
+        "{null: HTTP/1.1 200 Ok}{Foo: bar}{Bar: foo}{WWW-Authenticate: NTLM sdsds}{Bar: foo}",
+        "{null: HTTP/1.1 200 Ok}{WWW-Authenticate: Negotiate}{Foo: bar}{Bar: foo}{WWW-Authenticate: NTLM}{Bar: foo}{WWW-Authenticate: Kerberos}",
+        "{null: HTTP/1.1 200 Ok}{Foo: foo}{Bar: }{WWW-Authenticate: NTLM blob}{Bar: foo blob}"
+    };
+
+    static boolean[] expectedResult = {
+        false, false, true, true, true, false, false
+    };
+}