changeset 1743:81dffe63c913

6877357: IPv6 address does not work Reviewed-by: xuelei, alanb
author weijun
date Tue, 22 Sep 2009 10:01:32 +0800
parents 845fefff00a4
children 023063a403ed b8004f6f4812
files src/share/classes/sun/security/krb5/KrbKdcReq.java test/sun/security/krb5/IPv6.java
diffstat 2 files changed, 163 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/sun/security/krb5/KrbKdcReq.java	Mon Sep 21 23:01:42 2009 +0100
+++ b/src/share/classes/sun/security/krb5/KrbKdcReq.java	Tue Sep 22 10:01:32 2009 +0800
@@ -1,5 +1,5 @@
 /*
- * Portions Copyright 2000-2006 Sun Microsystems, Inc.  All Rights Reserved.
+ * Portions Copyright 2000-2009 Sun Microsystems, Inc.  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
@@ -149,6 +149,11 @@
                 send(realm,tempKdc,useTCP);
                 break;
             } catch (Exception e) {
+                if (DEBUG) {
+                    System.out.println(">>> KrbKdcReq send: error trying " +
+                            tempKdc);
+                    e.printStackTrace(System.out);
+                }
                 savedException = e;
             }
         }
@@ -179,10 +184,36 @@
         /*
          * Get port number for this KDC.
          */
-        StringTokenizer strTok = new StringTokenizer(tempKdc, ":");
-        String kdc = strTok.nextToken();
-        if (strTok.hasMoreTokens()) {
-            String portStr = strTok.nextToken();
+        String kdc = null;
+        String portStr = null;
+
+        if (tempKdc.charAt(0) == '[') {     // Explicit IPv6 in []
+            int pos = tempKdc.indexOf(']', 1);
+            if (pos == -1) {
+                throw new IOException("Illegal KDC: " + tempKdc);
+            }
+            kdc = tempKdc.substring(1, pos);
+            if (pos != tempKdc.length() - 1) {  // with port number
+                if (tempKdc.charAt(pos+1) != ':') {
+                    throw new IOException("Illegal KDC: " + tempKdc);
+                }
+                portStr = tempKdc.substring(pos+2);
+            }
+        } else {
+            int colon = tempKdc.indexOf(':');
+            if (colon == -1) {      // Hostname or IPv4 host only
+                kdc = tempKdc;
+            } else {
+                int nextColon = tempKdc.indexOf(':', colon+1);
+                if (nextColon > 0) {    // >=2 ":", IPv6 with no port
+                    kdc = tempKdc;
+                } else {                // 1 ":", hostname or IPv4 with port
+                    kdc = tempKdc.substring(0, colon);
+                    portStr = tempKdc.substring(colon+1);
+                }
+            }
+        }
+        if (portStr != null) {
             int tempPort = parsePositiveIntString(portStr);
             if (tempPort > 0)
                 port = tempPort;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/sun/security/krb5/IPv6.java	Tue Sep 22 10:01:32 2009 +0800
@@ -0,0 +1,127 @@
+/*
+ * Copyright 2009 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 6877357
+ * @summary IPv6 address does not work
+ */
+
+import com.sun.security.auth.module.Krb5LoginModule;
+import java.io.*;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import javax.security.auth.Subject;
+
+public class IPv6 {
+
+    public static void main(String[] args) throws Exception {
+
+        String[][] kdcs = {
+                {"simple.host", null},  // These are legal settings
+                {"simple.host", ""},
+                {"simple.host", "8080"},
+                {"0.0.0.1", null},
+                {"0.0.0.1", ""},
+                {"0.0.0.1", "8080"},
+                {"1::1", null},
+                {"[1::1]", null},
+                {"[1::1]", ""},
+                {"[1::1]", "8080"},
+                {"[1::1", null},        // Two illegal settings
+                {"[1::1]abc", null},
+        };
+        // Prepares a krb5.conf with every kind of KDC settings
+        PrintStream out = new PrintStream(new FileOutputStream("ipv6.conf"));
+        out.println("[libdefaults]");
+        out.println("default_realm = V6");
+        out.println("[realms]");
+        out.println("V6 = {");
+        for (String[] hp: kdcs) {
+            if (hp[1] != null) out.println("    kdc = "+hp[0]+":"+hp[1]);
+            else out.println("    kdc = " + hp[0]);
+        }
+        out.println("}");
+        out.close();
+
+        System.setProperty("sun.security.krb5.debug", "true");
+        System.setProperty("java.security.krb5.conf", "ipv6.conf");
+
+        ByteArrayOutputStream bo = new ByteArrayOutputStream();
+        PrintStream po = new PrintStream(bo);
+        PrintStream oldout = System.out;
+        System.setOut(po);
+
+        try {
+            Subject subject = new Subject();
+            Krb5LoginModule krb5 = new Krb5LoginModule();
+            Map<String, String> map = new HashMap<String, String>();
+            Map<String, Object> shared = new HashMap<String, Object>();
+
+            map.put("debug", "true");
+            map.put("doNotPrompt", "true");
+            map.put("useTicketCache", "false");
+            map.put("useFirstPass", "true");
+            shared.put("javax.security.auth.login.name", "any");
+            shared.put("javax.security.auth.login.password", "any".toCharArray());
+            krb5.initialize(subject, null, shared, map);
+            krb5.login();
+        } catch (Exception e) {
+            // Ignore
+        }
+
+        po.flush();
+
+        System.setOut(oldout);
+        String[] lines = new String(bo.toByteArray()).split("\n");
+        int cc = 0;
+        Pattern r = Pattern.compile(".*KrbKdcReq send: kdc=(.*) UDP:(\\d+),.*");
+        for (String line: lines) {
+            Matcher m = r.matcher(line.subSequence(0, line.length()));
+            if (m.matches()) {
+                System.out.println("------------------");
+                System.out.println(line);
+                String h = m.group(1), p = m.group(2);
+                String eh = kdcs[cc][0], ep = kdcs[cc][1];
+                if (eh.charAt(0) == '[') {
+                    eh = eh.substring(1, eh.length()-1);
+                }
+                System.out.println("Expected: " + eh + " : " + ep);
+                System.out.println("Actual: " + h + " : " + p);
+                if (!eh.equals(h) ||
+                        (ep == null || ep.length() == 0) && !p.equals("88") ||
+                        (ep != null && ep.length() > 0) && !p.equals(ep)) {
+                    throw new Exception("Mismatch");
+                }
+                cc++;
+            }
+        }
+        if (cc != kdcs.length - 2) {    // 2 illegal settings at the end
+            throw new Exception("Not traversed");
+        }
+    }
+}
+
+