// ----------------------------------------------------------
// File:	ni_krb4.c
//
// Contents:	main module for ni_krb4.dll.
//				ni_krb4.dll is the DLL to provide  
//				authentication module which authenticates on kerberos-5,
//				for ni_pam 
// 
// History:		7-2-97	Naomaru Itoi	created

#include "ni_nw.h"
// Global Valuables
HINSTANCE	hDllInstance;	// my instance
// functions

BOOL
WINAPI
DllMain(
		HINSTANCE hInstance,
		DWORD dwReason,
		LPVOID lpReserved)
{
    switch (dwReason)
    {
        case DLL_PROCESS_ATTACH:
            DisableThreadLibraryCalls ( hInstance );
            hDllInstance = hInstance;

        case DLL_PROCESS_DETACH:
        default:
            return(TRUE);
    }
}

BOOL
WINAPI
ni_sm_authenticate(NISTRUCT *niStruct)
{			
	WCHAR wcBuf[256];
	CHAR username[256], password[256];
	int ccode;
	NWDSContextHandle context;

	Buf_T *classBuffer;

	WideCharToMultiByte(CP_ACP,0,niStruct->username,-1,username,
						256,(LPCSTR)0,(LPBOOL)0);
	WideCharToMultiByte(CP_ACP,0,niStruct->password,-1,password,
						256,(LPCSTR)0,(LPBOOL)0);

	D(L"ni_nw : ni_sm_authenticate() called. \n");
	printf("ni_nw : username=%s\n", username);

	context = NWDSCreateContext();
	printf("context = %d\n", context);
	if (context == ERR_CONTEXT_CREATION)
	{
		printf("error : creating NWDS context (%d %i)\n",
			context, context);
		return (-1);
	}

	ccode = NWDSLogin(context, 0, username, password, 0);
	printf("NWDSLogin() returns %d\n", ccode);

	if (ccode == 0) {
		printf("NI_NW : SUCCESS\n");
	}
	else {
		printf("NI_NW : cannot login\n");
	}
	NWDSFreeContext(context);
	if (ccode==0) {
		wsprintf (wcBuf,L"%s: succeeded to login to netware\n", niStruct->username);
		NIError(wcBuf, L"NI_NW");
		return (NI_SUCCESS);
	}
	else {
		wsprintf (wcBuf,L"error : %s : failed to login to netware\n", niStruct->username);
		NIError(wcBuf, L"NI_NW");
		return (NI_FAILURE);	
	}
}

INT WINAPI ni_sm_logout()
{
	int ccode;
	NWDSContextHandle context;
	
	context = NWDSCreateContext();
	printf("NI_SM_LOGOUT : context = %d\n", context);
	ccode = NWDSLogout(context);
	printf("NWDSLogout() returns %d\n", ccode);
	NWDSFreeContext(context);
	if (ccode==0) return NI_SUCCESS;
	else return NI_FAILURE;
}

// ni_sm_chpass_check()
// preliminary check for change password protocol.
// 7-14-1997, Naomaru Itoi, created

INT WINAPI ni_sm_chpass_check(NISTRUCT *niStruct)
{
	INT rv;
	CHAR username[256], oldPassword[256];
	Buf_T *classBuffer;
	NWDSContextHandle context;

	context = NWDSCreateContext();

	WideCharToMultiByte(CP_ACP,0,niStruct->username,-1,username,
						256,(LPCSTR)0,(LPBOOL)0);
	WideCharToMultiByte(CP_ACP,0,niStruct->oldPassword,-1,oldPassword,
						256,(LPCSTR)0,(LPBOOL)0);

	//printf("verify ... %s, %s\n", username, oldPassword);
	rv = NWDSVerifyObjectPassword(context, 0, username, oldPassword);
	printf("NWDSVerifyObjectPassword() returns %d\n", rv);
	NWDSFreeContext(context);
	if (rv==0) return NI_SUCCESS;
	else return NI_FAILURE;
}

// ni_sm_chpass()
// change password function.
// 7-14-1997, Naomaru Itoi, created

INT WINAPI ni_sm_chpass(NISTRUCT *niStruct)
{
	INT rv;
	CHAR username[256], oldPassword[256], newPassword[256];
	NWDSContextHandle context;

	context = NWDSCreateContext();

	WideCharToMultiByte(CP_ACP,0,niStruct->username,-1,username,
						256,(LPCSTR)0,(LPBOOL)0);
	WideCharToMultiByte(CP_ACP,0,niStruct->oldPassword,-1,oldPassword,
						256,(LPCSTR)0,(LPBOOL)0);
	WideCharToMultiByte(CP_ACP,0,niStruct->newPassword,-1,newPassword,
						256,(LPCSTR)0,(LPBOOL)0);

	printf("change %s, %s, %s\n", username, oldPassword, newPassword);
	rv = NWDSChangeObjectPassword(context, 0, username, oldPassword, newPassword);
	printf("NWDSChangeObjectPassword() returns %d\n", rv);
	NWDSFreeContext(context);
	if (rv==0) return NI_SUCCESS;
	else return NI_FAILURE;
}

