/** * Dump a keytab: list all principals. * * @param keytabFile the keytab file * @throws IOException IO problems */ private void dumpKeytab(File keytabFile) throws IOException { title("Examining keytab %s", keytabFile); File kt = keytabFile.getCanonicalFile(); verifyFileIsValid(kt, CAT_KERBEROS, "keytab"); Keytab loadKeytab = Keytab.loadKeytab(kt); List<PrincipalName> principals = loadKeytab.getPrincipals(); println("keytab principal count: %d", principals.size()); int entrySize = 0; for (PrincipalName princ : principals) { List<KeytabEntry> entries = loadKeytab.getKeytabEntries(princ); entrySize = entrySize + entries.size(); for (KeytabEntry entry : entries) { EncryptionKey key = entry.getKey(); println(" %s: version=%d expires=%s encryption=%s", entry.getPrincipal(), entry.getKvno(), entry.getTimestamp(), key.getKeyType()); } } println("keytab entry count: %d", entrySize); endln(); }
@Test(timeout = 60000) public void testKeytabGen() throws Exception { MiniKdc kdc = getKdc(); File workDir = getWorkDir(); kdc.createPrincipal(new File(workDir, "keytab"), "foo/bar", "bar/foo"); List<PrincipalName> principalNameList = Keytab.loadKeytab(new File(workDir, "keytab")).getPrincipals(); Set<String> principals = new HashSet<String>(); for (PrincipalName principalName : principalNameList) { principals.add(principalName.getName()); } Assert.assertEquals(new HashSet<String>(Arrays.asList( "foo/bar@" + kdc.getRealm(), "bar/foo@" + kdc.getRealm())), principals); }
@Override public void removeKeytabEntries(PrincipalName principal, int kvno) { List<KeytabEntry> entries = getKeytabEntries(principal); for (KeytabEntry entry : entries) { if (entry.getKvno() == kvno) { removeKeytabEntry(entry); } } }
void writeBody(KeytabOutputStream kos, int version) throws IOException { kos.writePrincipal(principal, version); kos.writeTime(timestamp); kos.writeByte(kvno); kos.writeKey(key, version); }
void store(KeytabOutputStream kos) throws IOException { byte[] body = null; // compute entry body content first so that to get and write the size ByteArrayOutputStream baos = new ByteArrayOutputStream(); KeytabOutputStream subKos = new KeytabOutputStream(baos); writeBody(subKos, 0); // todo: consider the version subKos.flush(); body = baos.toByteArray(); kos.writeInt(body.length); kos.write(body); }
@Override public void store(OutputStream outputStream) throws IOException { if (outputStream == null) { throw new IllegalArgumentException("Invalid and null output stream"); } KeytabOutputStream kos = new KeytabOutputStream(outputStream); writeVersion(kos); writeEntries(kos); }
private void doLoad(KeytabInputStream kis) throws IOException { this.version = readVersion(kis); List<KeytabEntry> entries = readEntries(kis); addKeytabEntries(entries); }
private KeytabEntry readEntry(KeytabInputStream kis, int entrySize) throws IOException { KeytabEntry entry = new KeytabEntry(); entry.load(kis, version, entrySize); return entry; }
@Override public void writeCountedOctets(byte[] data) throws IOException { writeShort(data.length); write(data); } }
@Override public void addKeytabEntries(List<KeytabEntry> entries) { for (KeytabEntry entry : entries) { addEntry(entry); } }
@Override public int readOctetsCount() throws IOException { return readShort(); } }
private void writeVersion(KeytabOutputStream kos) throws IOException { byte[] bytes = new byte[2]; bytes[0] = (byte) 0x05; bytes[1] = version == V502 ? (byte) 0x02 : (byte) 0x01; kos.write(bytes); }
@Override public void removeKeytabEntries(PrincipalName principal, int kvno) { List<KeytabEntry> entries = getKeytabEntries(principal); for (KeytabEntry entry : entries) { if (entry.getKvno() == kvno) { removeKeytabEntry(entry); } } }
void writeBody(KeytabOutputStream kos, int version) throws IOException { kos.writePrincipal(principal, version); kos.writeTime(timestamp); kos.writeByte(kvno); kos.writeKey(key, version); }
@Override public void store(OutputStream outputStream) throws IOException { if (outputStream == null) { throw new IllegalArgumentException("Invalid and null output stream"); } KeytabOutputStream kos = new KeytabOutputStream(outputStream); writeVersion(kos); writeEntries(kos); }
private void doLoad(KeytabInputStream kis) throws IOException { this.version = readVersion(kis); List<KeytabEntry> entries = readEntries(kis); addKeytabEntries(entries); }
@Override public void writeCountedOctets(byte[] data) throws IOException { writeShort(data.length); write(data); } }
@Override public int readOctetsCount() throws IOException { return readShort(); } }