#pragma once

// Maximum privilege name length 
#define MAX_PRIV_LEN	5000
#define	MAX_ACCOUNTNAME_LEN	5000
#define MAX_DOMAINNAME_LEN	5000


// This will be the first call for every info we rip from the token
// makes sense to convert this into a function
DWORD GetTokenInformationLen(HANDLE processToken, TOKEN_INFORMATION_CLASS infoRequired) {

	DWORD structSize;

	GetTokenInformation(processToken, infoRequired, NULL, 0, &structSize);

	return structSize;

}

void PrintSIDType(SID_NAME_USE typeCheck) {

	switch (typeCheck)
	{

	case SidTypeUser:

		_tprintf(_T("SID Type: SidTypeUser: A user SID.\n"));
		break;

	case SidTypeGroup:
		_tprintf(_T("SID Type: SidTypeGroup: A group SID\n"));
		break;

	case SidTypeDomain:
		_tprintf(_T("SID Type: SidTypeDomain: A domain SID\n"));
		break;

	case SidTypeAlias:
		_tprintf(_T("SID Type: SidTypeAlias: An alias SID\n"));
		break;

	case SidTypeWellKnownGroup:
		_tprintf(_T("SID Type: SidTypeWellKnownGroup: A SID for a well-known group\n"));
		break;

	case SidTypeDeletedAccount:
		_tprintf(_T("SID Type: SidTypeDeletedAccount: A SID for a deleted account\n"));
		break;

	case SidTypeInvalid:
		_tprintf(_T("SID Type: SidTypeInvalid: A SID that is not valid\n"));
		break;

	case SidTypeUnknown:
		_tprintf(_T("SID Type: SidTypeUnknown: A SID of unknown type\n"));
		break;

	case SidTypeComputer:
		_tprintf(_T("SID Type: SidTypeComputer: A SID for a computer\n"));
		break;

	case SidTypeLabel:
		_tprintf(_T("SID Type: SidTypeLabel: A mandatory integrity label SID\n"));
		break;

	}
}

void PrintSidDetails(PSID psid) {


	LPTSTR stringSID = NULL;
	// Let's try and print the SID and the associated accounts
	// Well known: https://support.microsoft.com/en-in/help/243330/well-known-security-identifiers-in-windows-operating-systems

	// Convert SID to string 

	if (!ConvertSidToStringSid(psid, &stringSID)) {

		ErrorDetails(_T("ConvertSidToStringSid"), false);
	}



	// Let's also try and get the account name with the SID
	// LookupAccountSid https://msdn.microsoft.com/en-us/library/windows/desktop/aa379166(v=vs.85).aspx

	TCHAR accountName[MAX_ACCOUNTNAME_LEN];
	DWORD bufferLen = MAX_ACCOUNTNAME_LEN;
	TCHAR domainName[MAX_DOMAINNAME_LEN];
	DWORD domainNameBufferLen = MAX_DOMAINNAME_LEN;
	SID_NAME_USE sidType;

	if (!LookupAccountSid(NULL, psid, accountName, &bufferLen, domainName, &domainNameBufferLen, &sidType))
	{
		ErrorDetails(_T("LookupAccountSid"), false);
		return;
	}


	_tprintf(_T("SID: %s\n"), stringSID);
	PrintSIDType(sidType);
	_tprintf(_T("Account: %s\\%s\n"), domainName, accountName);


	LocalFree(stringSID);
}






