FileDocCategorySizeDatePackage
Ntlm.javaAPI DocJavaMail 1.4.36641Tue Nov 17 10:38:10 GMT 2009com.sun.mail.auth

Ntlm

public class Ntlm extends Object
NTLM authentication using reflection with the jcifs classes.
author
Luís Serralheiro

Fields Summary
private PrintStream
debugout
Constructors Summary
public Ntlm(PrintStream debugout)

        this.debugout = debugout;
        // NtlmPasswordAuthentication.debugout = debugout;
    
Methods Summary
public java.lang.StringgenerateType1Msg(boolean useUnicode, int flags, java.lang.String domain, java.lang.String workstation)

        try {
            Class t1MClass = Class.forName("jcifs.ntlmssp.Type1Message");
	    Constructor t1Mconstructor = t1MClass.getConstructor(
		new Class[] { int.class, String.class, String.class });
	    Object t1m = t1Mconstructor.newInstance(
		new Object[] { new Integer(flags), domain, workstation });
	    if (debugout != null)
		debugout.println("DEBUG NTLM: type 1 message: " + t1m);
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            BASE64EncoderStream out =
                    new BASE64EncoderStream(bout, Integer.MAX_VALUE);
	    if (debugout != null)
		debugout.println("DEBUG NTLM: type 1 message length: " +
		    ((byte[]) t1MClass.getMethod("toByteArray",
			new Class[0]).invoke(t1m, null)).length);
            out.write((byte[]) t1MClass.getMethod("toByteArray",
                    new Class[0]).invoke(t1m, null));
            out.flush();
            out.close();
            return new String(bout.toByteArray());
        } catch (IOException ioex) {
	    if (debugout != null)
		ioex.printStackTrace(debugout);
	    throw ioex;		// pass it on
        } catch (Exception ex) {
	    if (debugout != null)
		ex.printStackTrace(debugout);
        }
        return null;
    
public java.lang.StringgenerateType3Msg(java.lang.String username, java.lang.String password, java.lang.String domain, java.lang.String workstation, java.lang.String challenge, int flags, int lmCompatibility)

        try {
            Object t2m;
            Class t2MClass = Class.forName("jcifs.ntlmssp.Type2Message");
            Constructor t2Mconstructor =
                    t2MClass.getConstructor(new Class[]{Class.forName("[B")});
            try {
                BASE64DecoderStream in = new BASE64DecoderStream(
                        new ByteArrayInputStream(challenge.getBytes()));
                byte[] bytes = new byte[in.available()];
                in.read(bytes);
                t2m = t2Mconstructor.newInstance(new Object[]{bytes});
            } catch (IOException ex) {
                IOException ioex = new IOException("Invalid Type2 message");
		ioex.initCause(ex);
                throw ioex;
            }
            Class t3MClass = Class.forName("jcifs.ntlmssp.Type3Message");
            Constructor t3Mconstructor = t3MClass.getConstructor(
                    new Class[]{
                        t2MClass,
                        String.class,
                        String.class,
                        String.class,
                        String.class,
			int.class});
            Object t3m = t3Mconstructor.newInstance(new Object[]{
                        t2m,
                        password,
                        domain == null ? "" : domain,
                        username,
                        workstation,
			new Integer(flags)});

            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            BASE64EncoderStream out =
                    new BASE64EncoderStream(bout, Integer.MAX_VALUE);
            out.write((byte[]) t3MClass.getMethod("toByteArray",
                    new Class[0]).invoke(t3m, null));
            out.flush();
            out.close();
            return new String(bout.toByteArray());
        } catch (InvocationTargetException itex) {
	    if (debugout != null)
		itex.printStackTrace(debugout);
	    Throwable t = itex.getTargetException();
	    if (t instanceof IOException)
		throw (IOException)t;
	    IOException ioex = new IOException(
				"Ntlm.generateType3Msg failed" +
				"; Exception: " + t);
	    ioex.initCause(itex);
	    throw ioex;
        } catch (IOException ioex) {
	    if (debugout != null)
		ioex.printStackTrace(debugout);
	    throw ioex;		// pass it on
        } catch (Exception ex) {
	    if (debugout != null)
		ex.printStackTrace(debugout);
        }
        return null;