in kerby-tool/client-tool/src/main/java/org/apache/kerby/kerberos/tool/klist/KlistTool.java [184:234]
private static int printKeytabInfo(KOptions klOptions) {
String[] header = new String[4];
header[0] = "KVNO Principal\n"
+ "---- --------------------------------------------------------------------------";
header[1] = header[0];
header[2] = "KVNO Timestamp Principal\n"
+ "---- ------------------- ------------------------------------------------------";
header[3] = header[2];
int outputIndex = 0;
if (klOptions.contains(KlistOption.SHOW_KTAB_ENTRY_TS)) {
outputIndex |= 2;
}
if (klOptions.contains(KlistOption.SHOW_KTAB_ENTRY_KEY)) {
outputIndex |= 1;
}
System.out.println("Keytab name: FILE:" + keytabFilePath);
try {
File keytabFile = new File(keytabFilePath);
if (!keytabFile.exists()) {
System.out.println("klist: Key table file '" + keytabFilePath + "' not found. ");
return 0;
}
System.out.println(header[outputIndex]);
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss", Locale.US);
Keytab keytab = Keytab.loadKeytab(keytabFile);
List<PrincipalName> principals = keytab.getPrincipals();
for (PrincipalName principal : principals) {
List<KeytabEntry> keytabEntries = keytab.getKeytabEntries(principal);
for (KeytabEntry entry : keytabEntries) {
StringBuilder sb = new StringBuilder();
sb.append(String.format("%-4d ", entry.getKvno()));
if ((outputIndex & 2) != 0) {
Date date = new Date(entry.getTimestamp().getTime());
sb.append(format.format(date));
sb.append(' ');
}
sb.append(String.format("%s ", principal.getName()));
if ((outputIndex & 1) != 0) {
sb.append("(0x");
sb.append(HexUtil.bytesToHex(entry.getKey().getKeyData()));
sb.append(")");
}
System.out.println(sb);
}
}
} catch (IOException e) {
System.err.println("klist: Error while scan key table file '" + keytabFilePath + "'");
}
return 0;
}