jdk/test/sun/security/krb5/auto/KDC.java
changeset 2942 37d9baeb7518
parent 1575 e0f1979051b5
child 3046 dd50d75d88e6
--- a/jdk/test/sun/security/krb5/auto/KDC.java	Thu Jun 04 11:28:03 2009 +0800
+++ b/jdk/test/sun/security/krb5/auto/KDC.java	Tue Jun 09 14:17:05 2009 +0800
@@ -1,5 +1,5 @@
 /*
- * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
+ * Copyright 2008-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
@@ -201,14 +201,14 @@
     }
 
     /**
-     * Write all principals' keys into a keytab file. Note that the keys for
-     * the krbtgt principal for this realm will not be written.
+     * Write all principals' keys from multiple KDCsinto one keytab file.
+     * Note that the keys for the krbtgt principals will not be written.
      * <p>
      * Attention: This method references krb5.conf settings. If you need to
      * setup krb5.conf later, please call <code>Config.refresh()</code> after
      * the new setting. For example:
      * <pre>
-     * kdc.writeKtab("/etc/kdc/ktab");  // Config is initialized,
+     * KDC.writeKtab("/etc/kdc/ktab", kdc);  // Config is initialized,
      * System.setProperty("java.security.krb5.conf", "/home/mykrb5.conf");
      * Config.refresh();
      * </pre>
@@ -223,21 +223,32 @@
      * @throws sun.security.krb5.KrbException for any realm and/or principal
      *         name error.
      */
-    public void writeKtab(String tab) throws IOException, KrbException {
+    public static void writeMultiKtab(String tab, KDC... kdcs)
+            throws IOException, KrbException {
         KeyTab ktab = KeyTab.create(tab);
-        for (String name : passwords.keySet()) {
-            if (name.equals("krbtgt/" + realm)) {
-                continue;
+        for (KDC kdc: kdcs) {
+            for (String name : kdc.passwords.keySet()) {
+                if (name.equals("krbtgt/" + kdc.realm)) {
+                    continue;
+                }
+                ktab.addEntry(new PrincipalName(name + "@" + kdc.realm,
+                        name.indexOf('/') < 0 ?
+                            PrincipalName.KRB_NT_UNKNOWN :
+                            PrincipalName.KRB_NT_SRV_HST),
+                            kdc.passwords.get(name));
             }
-            ktab.addEntry(new PrincipalName(name + "@" + realm,
-                    name.indexOf('/') < 0 ?
-                        PrincipalName.KRB_NT_UNKNOWN :
-                        PrincipalName.KRB_NT_SRV_HST), passwords.get(name));
         }
         ktab.save();
     }
 
     /**
+     * Write a ktab for this KDC.
+     */
+    public void writeKtab(String tab) throws IOException, KrbException {
+        KDC.writeMultiKtab(tab, this);
+    }
+
+    /**
      * Adds a new principal to this realm with a given password.
      * @param user the principal's name. For a service principal, use the
      *        form of host/f.q.d.n