package com.novell.security.nmas.lcm.clrpwd;
import com.novell.security.nmas.lcm.*;
import com.novell.security.nmas.transport.*;
import com.novell.security.nmas.client.*;
import com.novell.security.nmas.io.*;
import com.novell.security.nmas.*;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.UnsupportedCallbackException;
import javax.security.auth.callback.PasswordCallback;
import java.io.*;
public class ClearPasswordLCM implements LCM
{
private final static String METHOD_NAME = "Clear Password";
private final static int SIZEOF_REPLY = 4;
private final static Integer METHOD_ID = new Integer(1);
MAF maf = null;
CallbackHandler callbackHandler = null;
public Integer getMethodID()
{
return METHOD_ID;
}
public String getMethodName()
{
return METHOD_NAME;
}
public int doMethod(MAF maf, CallbackHandler callbackHdlr)
{
this.maf = maf;
this.callbackHandler = callbackHdlr;
String password = null;
boolean success = false;
int err;
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
NMASDataOutputStream dataOut = new NMASDataOutputStream(byteOut);
err = maf.begin();
if(err != 0) return err;
try
{
password = getPwd();
}
catch (IOException ioe)
{
return NMASConstants.NMAS_E_LOGIN_FAILED;
}
try
{
dataOut.writeLEInt(0);
dataOut.writeLEInt(0);
dataOut.flush();
err = maf.write(byteOut.toByteArray());
}
catch(IOException e)
{
methodError(NMASConstants.NMAS_E_TRANSPORT);
return NMASConstants.NMAS_E_TRANSPORT;
}
catch(MAFTransportException e)
{
methodError( e.getErrorCode() );
return e.getErrorCode();
}
if(err != 0)
{
maf.end(err, 0, null);
return err;
}
byteOut.reset();
try
{
password = getPwd();
}
catch(IOException ioe)
{
err = NMASConstants.NMAS_E_INVALID_OPERATION;
maf.end(err, 0, null);
return err;
}
byte[] replyData = null;
byte[] pwdBytes = null;
try
{
dataOut.writeUTF(password);
DataInputStream dStrip = new DataInputStream(new ByteArrayInputStream(byteOut.toByteArray()));
short byteCount = dStrip.readShort();
pwdBytes = new byte[byteCount];
dStrip.readFully(pwdBytes);
err = maf.xWrite(pwdBytes);
if(err != NMASConstants.NMAS_SUCCESS)
{
methodError(err);
return err;
}
replyData = maf.read(SIZEOF_REPLY);
}
catch(IOException e)
{
methodError( NMASConstants.NMAS_E_TRANSPORT );
return NMASConstants.NMAS_E_TRANSPORT;
}
catch(NMASEncryptionException nee)
{
methodError( NMASConstants.NMAS_E_CRYPTO_FAILED_INIT );
return NMASConstants.NMAS_E_CRYPTO_FAILED_INIT;
}
catch(MAFTransportException e)
{
methodError(e.getErrorCode());
return e.getErrorCode();
}
if(replyData != null && replyData.length > 3
&& (replyData[0] != 0 || replyData[1] != 0 || replyData[2] != 0 || replyData[3] != 0 ))
{
success = true;
}
else
{
success = false;
}
if(!success)
{
methodError(NMASConstants.NMAS_E_LOGIN_FAILED);
}
else
{
maf.end(0, 0, null);
}
return 0;
}
private String getPwd() throws IOException
{
String pwd = null;
pwd = maf.getAttribute(MAF.NMAS_AID_PASSWORD);
if(pwd != null)
{
return pwd;
}
Callback [] callbacks = new Callback[1];
PasswordCallback pwdCallback = new PasswordCallback("Password: ", true);
callbacks[0] = pwdCallback;
try
{
callbackHandler.handle(callbacks);
pwd = new String(((PasswordCallback)callbacks[0]).getPassword());
}
catch (IOException e)
{
e.printStackTrace();
throw e;
}
catch (UnsupportedCallbackException ue)
{
ue.printStackTrace();
throw new IOException();
}
if(pwd == null)
{
return "";
}
return pwd;
}
private void methodError(int errorCode)
{
maf.end( errorCode, 0, null );
}
}