#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#ifdef WIN32
#include <windows.h>
#else
#include <signal.h>
#endif
#include "userlib.h"
#include "defines.h"

#ifndef LOGON32_LOGON_NETWORK
#define LOGON32_LOGON_NETWORK 3
#endif

int user_lookup(char *xuser, char *xpass, char *groups[], int *ngroups, int maxg, char *ip, char *vdomain, char *ntdomain, char *msg)
{
#ifdef WIN32
	return user_nt_lookup(xuser,xpass,groups,ngroups,maxg,ip,vdomain,ntdomain,msg);
#else
	return user_unix_lookup(xuser,xpass,groups,ngroups,maxg,ip,vdomain,ntdomain,msg);
#endif
}
#ifdef WIN32
int user_nt_lookup(char *xuser, char *xpass, char *groups[], int *ngroups, int maxg, char *ip, char *vdomain, char *ntdomain, char *msg)
{
	char user[BFSZ],pass[BFSZ];
        HANDLE token;
	int ltype;
	int rr,r;
	int domain_login = FALSE;

	strncpy(user,xuser,100);user[100] = 0;
	strncpy(pass,xpass,100);pass[100] = 0;
	strcpy(msg,"");
	ltype = LOGON32_LOGON_NETWORK;

	r = LogonUser(user,ntdomain,pass,ltype,LOGON32_PROVIDER_DEFAULT,&token);
        if (!r) {
                rr = GetLastError();
                if (rr==ERROR_LOGON_TYPE_NOT_GRANTED) sprintf(msg,"Logon type not granted, User must have Logon Locally access in their profile");
                else if (rr==ERROR_PRIVILEGE_NOT_HELD) sprintf(msg,"Error, priv not held, you need to run dnews with 'may act as part of the system' priv");
                else sprintf(msg,"Err %d NT logonuser failed for (%s)\n",rr,user);
        }
	if (r) CloseHandle(token);
	return r;
}
#endif
