Click to See Complete Forum and Search --> : LogonUser() failed


ace_maggot
March 22nd, 2005, 02:45 AM
HI,

In my project, I need to logon programmatically on the local machine.

I am using "LogonUser()" api. The calling process must have SE_TCB_NAME privilege enabled. I dont how to get that privilege.

For that I have written following code.

//Start
if(!OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hCurrentToken))
printf("\n\nError Code for OpenProcessToken : %u",GetLastError());
else
printf("\nToken obtained");

if(LookupPrivilegeValue(NULL, SE_TCB_NAME,
&tkp1.Privileges[0].Luid))
printf("\nLookup Privilege value TCB returned true");

if(LookupPrivilegeValue(NULL, SE_CHANGE_NOTIFY_NAME,
&tkp2.Privileges[0].Luid))
printf("\nLookup Privilege value CHANGE returned true"); ;

tkp1.PrivilegeCount = 1;
tkp2.PrivilegeCount = 1;
tkp1.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
tkp2.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

if(!AdjustTokenPrivileges(hCurrentToken, FALSE, &tkp1, 0,
(PTOKEN_PRIVILEGES)NULL, 0))
printf("\nCan't set privileges required in tkp1");

dwErr = GetLastError();

if((dwErr != ERROR_SUCCESS))
printf("Error code for tkp1 : %u",dwErr);

if(!AdjustTokenPrivileges(hCurrentToken, FALSE, &tkp2, 0,
(PTOKEN_PRIVILEGES)NULL, 0))
printf("\nCan't set privileges required in tkp2");

dwErr = GetLastError();
if(dwErr != ERROR_SUCCESS)
printf("\nCan't set privileges required in tkp2");


strcpy(pchUserName,"user1");
strcpy(pchPasswd,"user1");
strcpy(pchDomain,"MYDOMAIN");


if(!LogonUser(pchUserName,pchDomain,pchPasswd,LOGON32_LOGON_INTERACTIVE
,LOGON32_PROVIDER_DEFAULT,&hToken))
printf("\nLogon failed :\n\n ");

//End

Output :

Token obtained
Lookup Privilege value TCB returned true
Lookup Privilege value CHANGE returned true

Error code for tkp1 : 1300
Logon failed :
**************************************************

Please help. How to set SE_TCB_NAME privilege and how to programmatically login on to the local computer.

AdaraCD
March 22nd, 2005, 03:55 AM
Call GetLastError() after the call to AdjustTokenPrivileges(), if the return code is ERROR_NOT_ALL_ASSIGNED you need to add the privileges to your access token with LsaAddAccountRights() before enabling them

Andreas Masur
March 22nd, 2005, 06:08 AM
A SDK function (e.g. CreateDirectory) failed. What I'm doing wrong? (http://www.codeguru.com/forum/showthread.php?t=318721)