directory-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Wang, Xunhua - wangxx" <wan...@jmu.edu>
Subject java.nio.BufferUnderflowException and org.apache.directory.server.kerberos.shared.keytab.KeytabDecoder
Date Sat, 03 Mar 2012 14:19:08 GMT
Hi,

I am trying to use the KeytabDecoder class (in org.apache.directory.server.kerberos.shared.keytab)
to read the content of a keytab file generated by a MS Active Directory server. I used the
following code and got a java.nio.BufferUnderflowException.

+++++ CODE BEGINS +++++
import java.io.*;
import java.util.*;
import java.nio.*;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.directory.server.kerberos.shared.keytab.*;
import org.apache.directory.server.kerberos.shared.crypto.encryption.*;
import org.apache.directory.shared.kerberos.*;
import org.apache.directory.shared.kerberos.components.*;
import org.apache.directory.shared.kerberos.codec.*;
import org.apache.directory.shared.kerberos.codec.types.*;
import org.apache.directory.shared.util.Strings;

public class SimpleKeytabDecoder {
	public void test (String filename) throws Exception {
		FileInputStream fis = new FileInputStream (filename);
		int length = fis.available ();
		byte[] data = new byte[length];

		fis.read (data);
		
		IoBuffer ioBuf = IoBuffer.wrap (data);

		KeytabDecoder kd = new KeytabDecoder ();
		List<KeytabEntry> list = kd.getKeytabEntries (ioBuf);
		Iterator itr = list.iterator ();
		while (itr.hasNext ()) {
			KeytabEntry ke = (KeytabEntry) itr.next ();

			printOut (ke);
		}

		fis.close ();
	}

	private void printOut (KeytabEntry keytab) {
		StringBuffer sb = new StringBuffer ();

		String principalName = keytab.getPrincipalName ();
		long principalType = keytab.getPrincipalType ();

		sb.append ("Principal name = " + principalName + "\r\n");
		sb.append ("Principal type = " + principalType + "\r\n");

		EncryptionKey ek = keytab.getKey ();
		EncryptionType et = ek.getKeyType ();
		sb.append (et.toString() + "\r\n");
		byte[] keyValue = ek.getKeyValue ();
		sb.append ("Key value = " + Strings.toHexString (keyValue));

		int keyVer1 = ek.getKeyVersion ();
		sb.append ("Key version = " + keyVer1 + "\r\n");

		byte keyVer2 = keytab.getKeyVersion ();
		sb.append ("Key version 2 = " + keyVer2 + "\r\n");

		KerberosTime kt = keytab.getTimeStamp ();
		sb.append ("Time = " + kt.getTime () + "\r\n");
		sb.append ("Date = " + kt.toDate ().toString() + "\r\n");

		System.out.println (sb.toString ());
	}

	public static void main (String[] args) {

		try {
			if (args.length < 1) {
				System.out.println ("Use java SimpleKeytabDecoder filename");
				return;
			}
			
			SimpleKeytabDecoder skd = new SimpleKeytabDecoder ();

			System.out.println ("Keytab filename = " + args[0]);

			skd.test (args[0]);
		} catch (Exception ex) {
			ex.printStackTrace ();
		}
	}
}
+++++ CODE ENDS +++++

The exact exception is

+++++ Exception Begins +++++
java.nio.BufferUnderflowException
        at java.nio.HeapByteBuffer.get(Unknown Source)
        at org.apache.mina.core.buffer.AbstractIoBuffer.get(AbstractIoBuffer.java:617)
        at org.apache.mina.core.buffer.AbstractIoBuffer.get(AbstractIoBuffer.java:1324)
        at org.apache.directory.server.kerberos.shared.keytab.KeytabDecoder.getKeytabEntries(KeytabDecoder.java:69)
        at SimpleKeytabDecoder.test(SimpleKeytabDecoder.java:25)
        at SimpleKeytabDecoder.main(SimpleKeytabDecoder.java:76)
+++++ Exception Ends +++++

Am I using the wrong class? Is keytab of Apacheds compatible with MS Keytab?

Thank you.

Steve
Mime
View raw message