I have used the below code and it is working fine for most of the cases but for one windows machine it is failing at the section where we call 'GetTokenInformation' win32 API function and throws an error as ERROR: API = GetTokenInformation, error code = 1312, message = A specified logon session does not exist. It may already have been terminated.
)
What could have gone wrong and how can we resolve this. Please help. Thanks!
// Logging on as the passed ssh user (not logged in user) which is non-Administrator
if (!LogonUser(username.c_str(), domain.c_str(), password.c_str(), LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, &hToken)) {
DisplayError(L"LogonUser");
goto Cleanup;
}
tokenUsed = hToken;
// If the ssh user (not logged in user) is non-Administrator, we need linked token
if (!adminUser)
{
if (!GetTokenInformation(hToken, TokenLinkedToken, &linkedToken, sizeof(TOKEN_LINKED_TOKEN), &returnLength))
{
DisplayError(L"GetTokenInformation");
goto Cleanup;
}
// Duplicate the token to ensure valid usage
if (!DuplicateTokenEx(linkedToken.LinkedToken, MAXIMUM_ALLOWED, NULL, SecurityIdentification, TokenPrimary, &hDupToken)) {
DisplayError(L"DuplicateTokenEx");
goto Cleanup;
}
tokenUsed = hDupToken; // Use the duplicated token
}