jdk/src/share/classes/sun/security/krb5/internal/ktab/KeyTab.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 3317 a1ea2f1893f9
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Portions Copyright 2000-2006 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *  (C) Copyright IBM Corp. 1999 All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *  Copyright 1997 The Open Group Research Institute.  All rights reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
package sun.security.krb5.internal.ktab;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.krb5.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.krb5.internal.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.security.krb5.internal.crypto.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.io.FileInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.io.FileOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * This class represents key table. The key table functions deal with storing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * and retrieving service keys for use in authentication exchanges.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @author Yanni Zhang
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
public class KeyTab implements KeyTabConstants {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    int kt_vno;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static KeyTab singleton = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private static final boolean DEBUG = Krb5.DEBUG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private static String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private Vector<KeyTabEntry> entries = new Vector<KeyTabEntry> ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private KeyTab(String filename) throws IOException, RealmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        init(filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public static KeyTab getInstance(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        name = parse(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            return getInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        return getInstance(new File(name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * Gets the single instance of KeyTab class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * @param file the key tab file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * @return single instance of KeyTab;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     *  return null if error occurs while reading data out of the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public static KeyTab getInstance(File file) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            if (!(file.exists())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                singleton = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                String fname = file.getAbsolutePath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                // Since this class deals with file I/O operations,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                // we want only one class instance existing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                if (singleton != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                    File kfile = new File(singleton.name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                    String kname = kfile.getAbsolutePath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                    if (kname.equalsIgnoreCase(fname)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                       if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                          System.out.println("KeyTab instance already exists");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    singleton = new KeyTab(fname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            singleton = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                System.out.println("Could not obtain an instance of KeyTab" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                                   e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        return singleton;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Gets the single instance of KeyTab class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @return single instance of KeyTab; return null if default keytab file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *  does not exist, or error occurs while reading data from the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    public static KeyTab getInstance() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            name = getDefaultKeyTab();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            if (name != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                singleton = getInstance(new File(name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            singleton = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                System.out.println("Could not obtain an instance of KeyTab" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                                   e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        return singleton;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * The location of keytab file will be read from the configuration file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * If it is not specified, consider user.home as the keytab file's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * default location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    private static String getDefaultKeyTab() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        if (name != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            String kname = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                String keytab_names = Config.getInstance().getDefault
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                    ("default_keytab_name", "libdefaults");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                if (keytab_names != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                    StringTokenizer st = new StringTokenizer(keytab_names, " ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                    while (st.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                        kname = parse(st.nextToken());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                        if (kname != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            } catch (KrbException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                kname = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            if (kname == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                String user_home =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                        java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                        new sun.security.action.GetPropertyAction("user.home"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                if (user_home == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    user_home =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                        java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                        new sun.security.action.GetPropertyAction("user.dir"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                if (user_home != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                    kname = user_home + File.separator  + "krb5.keytab";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            return kname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    private static String parse(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        String kname = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        if ((name.length() >= 5) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            (name.substring(0, 5).equalsIgnoreCase("FILE:"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            kname = name.substring(5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        } else if ((name.length() >= 9) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                (name.substring(0, 9).equalsIgnoreCase("ANY:FILE:"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            // this format found in MIT's krb5.ini.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            kname = name.substring(9);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        } else if ((name.length() >= 7) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                (name.substring(0, 7).equalsIgnoreCase("SRVTAB:"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            // this format found in MIT's krb5.ini.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            kname = name.substring(7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            kname = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        return kname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    private synchronized void init(String filename)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        throws IOException, RealmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        if (filename != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            KeyTabInputStream kis =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                new KeyTabInputStream(new FileInputStream(filename));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            load(kis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            kis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            name = filename;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    private void load(KeyTabInputStream kis)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        throws IOException, RealmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        entries.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        kt_vno = kis.readVersion();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        if (kt_vno == KRB5_KT_VNO_1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            kis.setNativeByteOrder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        int entryLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        KeyTabEntry entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        while (kis.available() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            entryLength = kis.readEntryLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            entry = kis.readEntry(entryLength, kt_vno);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                System.out.println(">>> KeyTab: load() entry length: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                        entryLength + "; type: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                        (entry != null? entry.keyType : 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            if (entry != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                entries.addElement(entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * Reads the service key from the keytab file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * @param service the PrincipalName of the requested service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @return the last service key in the keytab
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    public EncryptionKey readServiceKey(PrincipalName service) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        KeyTabEntry entry = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        if (entries != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            // Find latest entry for this service that has an etype
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            // that has been configured for use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            for (int i = entries.size()-1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                entry = entries.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                if (entry.service.match(service)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    if (EType.isSupported(entry.keyType)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                        return new EncryptionKey(entry.keyblock,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                                             entry.keyType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                                             new Integer(entry.keyVersion));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                    } else if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                        System.out.println("Found unsupported keytype (" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                            entry.keyType + ") for " + service);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * Reads all keys for a service from the keytab file that have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * etypes that have been configured for use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @param service the PrincipalName of the requested service
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @return an array containing all the service keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    public EncryptionKey[] readServiceKeys(PrincipalName service) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        KeyTabEntry entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        EncryptionKey key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        int size = entries.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        ArrayList<EncryptionKey> keys = new ArrayList<EncryptionKey> (size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        if (entries != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            for (int i = size-1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                entry = entries.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                if (entry.service.match(service)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                    if (EType.isSupported(entry.keyType)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                        key = new EncryptionKey(entry.keyblock,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                                            entry.keyType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                                            new Integer(entry.keyVersion));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                        keys.add(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                            System.out.println("Added key: " + entry.keyType +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                                "version: " + entry.keyVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                    } else if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                        System.out.println("Found unsupported keytype (" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                            entry.keyType + ") for " + service);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        size = keys.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        if (size == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        EncryptionKey[] retVal = new EncryptionKey[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        // Sort keys according to default_tkt_enctypes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        int pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        EncryptionKey k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            System.out.println("Ordering keys wrt default_tkt_enctypes list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        int[] etypes = EType.getDefaults("default_tkt_enctypes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        if (etypes == null || etypes == EType.getBuiltInDefaults()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            // Either no supported types specified in default_tkt_enctypes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            // or no default_tkt_enctypes entry at all. For both cases,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            // just return supported keys in the order retrieved
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            for (int i = 0; i < size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                retVal[pos++] = keys.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            for (int j = 0; j < etypes.length && pos < size; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                int target = etypes[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                for (int i = 0; i < size && pos < size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                    k = keys.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                    if (k != null && k.getEType() == target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                            System.out.println(pos + ": " + k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                        retVal[pos++] = k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                        keys.set(i, null);  // Cleared from consideration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            // copy the rest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            for (int i = 0; i < size && pos < size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                k = keys.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                if (k != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                    retVal[pos++] = k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        if (pos != size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                "Internal Error: did not copy all keys;expecting " + size +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                    "; got " + pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        return retVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * Searches for the service entry in the keytab file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * The etype of the key must be one that has been configured
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * to be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * @param service the PrincipalName of the requested service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * @return true if the entry is found, otherwise, return false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    public boolean findServiceEntry(PrincipalName service) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        KeyTabEntry entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        if (entries != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            for (int i = 0; i < entries.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                entry = entries.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                if (entry.service.match(service)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                    if (EType.isSupported(entry.keyType)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                    } else if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                        System.out.println("Found unsupported keytype (" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                            entry.keyType + ") for " + service);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    public static String tabName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * Adds a new entry in the key table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * @param service the service which will have a new entry in the key table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * @param psswd the password which generates the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    public void addEntry(PrincipalName service, char[] psswd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
         throws KrbException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        EncryptionKey[] encKeys = EncryptionKey.acquireSecretKeys(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            psswd, service.getSalt());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        for (int i = 0; encKeys != null && i < encKeys.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            int keyType = encKeys[i].getEType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            byte[] keyValue = encKeys[i].getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            int result = retrieveEntry(service, keyType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            int kvno = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            if (result != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                KeyTabEntry oldEntry = entries.elementAt(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                kvno = oldEntry.keyVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                entries.removeElementAt(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                kvno += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                kvno = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            KeyTabEntry newEntry = new KeyTabEntry(service,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                            service.getRealm(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                            new KerberosTime(System.currentTimeMillis()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                                               kvno, keyType, keyValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            if (entries == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                entries = new Vector<KeyTabEntry> ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            entries.addElement(newEntry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * Retrieves the key table entry with the specified service name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * @param service the service which may have an entry in the key table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * @return -1 if the entry is not found, else return the entry index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * in the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    private int retrieveEntry(PrincipalName service, int keyType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        int found = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        KeyTabEntry e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        if (entries != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            for (int i = 0; i < entries.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                e = entries.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                if (service.match(e.getService()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                    (keyType == -1 || e.keyType == keyType)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                    return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        return found;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * Gets the list of service entries in key table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * @return array of <code>KeyTabEntry</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    public KeyTabEntry[] getEntries() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        if (entries != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            KeyTabEntry[] kentries = new KeyTabEntry[entries.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            for (int i = 0; i < kentries.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                kentries[i] = entries.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            return kentries;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * Creates a new default key table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    public synchronized static KeyTab create()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        throws IOException, RealmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        String dname = getDefaultKeyTab();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        return create(dname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * Creates a new default key table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    public synchronized static KeyTab create(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        throws IOException, RealmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        KeyTabOutputStream kos =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                new KeyTabOutputStream(new FileOutputStream(name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        kos.writeVersion(KRB5_KT_VNO);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        kos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        singleton = new KeyTab(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        return singleton;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * Saves the file at the directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    public synchronized void save() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        KeyTabOutputStream kos =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                new KeyTabOutputStream(new FileOutputStream(name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        kos.writeVersion(kt_vno);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        for (int i = 0; i < entries.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            kos.writeEntry(entries.elementAt(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        kos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * Removes an entry from the key table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * @param service the service <code>PrincipalName</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    public void deleteEntry(PrincipalName service) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        int result = retrieveEntry(service, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        if (result != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            entries.removeElementAt(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * Creates key table file version.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * @param file the key table file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * @exception IOException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    public synchronized void createVersion(File file) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        KeyTabOutputStream kos =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                new KeyTabOutputStream(new FileOutputStream(file));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        kos.write16(KRB5_KT_VNO);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        kos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    public static void refresh() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        if (singleton != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                System.out.println("Refreshing Keytab");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            singleton = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
}