🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.nativeapi
2 messages
2 total messages Started by Maurice Anderson Sat, 17 Mar 2007 22:31
Both OpenThreadToken & OpenProcessToken Fails
#2258
Author: Maurice Anderson
Date: Sat, 17 Mar 2007 22:31
116 lines
4881 bytes
Hello,

For the purposes of connecting to a remote registry, I wrote the function 
below to check if the current loginID has a certain priviledge, in this case 
SE_TCB_NAME.

The problem is OpenThreadToken fails with "An attempt was made to reference 
a token that does not exist".  And OpenProcessToken fails with "Access 
Denied".  Any ideas?  Is my code up to par with what I am trying to 
accomplish(connecting to the remote registry)?  Any improvement suggestions?

Thanks

bool __fastcall TRegIniFilePlus::IsTokenPriviledges(char* Priviledge)
{

unsigned long dwLength, dwPrivilegeNameSize, dwDisplayNameSize, dwLangId;
PTOKEN_GROUPS ptg = NULL;
UCHAR InfoBuffer[1000];
PTOKEN_PRIVILEGES ptgPrivileges = NULL;
LUID setcbnameValue;

bool Status = false;
HANDLE hToken;
UCHAR ucPrivilegeName[500];
UCHAR ucDisplayName[500];
String DisplayName;

        if(!OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, true, &hToken))
        {
           PrintError(GetLastError());
           Form1->Memo1->Lines->Add(FLastError + " OpenThreadToken: 
Failed");
           if(OpenProcessToken(GetCurrentProcess(),
                TOKEN_READ, &hToken))
           {
                PrintError(GetLastError());
                Form1->Memo1->Lines->Add(FLastError + " OpenProcessToken: 
Failed");
            return false;
           }
        }

        if (!GetTokenInformation(
                hToken,                              // handle to the access 
token
                TokenGroups,                    // get information about the 
token's groups
                (LPVOID) ptgPrivileges,    // pointer to TOKEN_GROUPS buffer
                0,                                       // size of buffer
                &dwLength                        // receives required buffer 
size
            ))
        {
                Status = false;

                if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
                    goto Cleanup;

                ptg = (PTOKEN_GROUPS)HeapAlloc(GetProcessHeap(),
                        HEAP_ZERO_MEMORY, dwLength);

                if (ptgPrivileges == NULL)
                    goto Cleanup;
        }

        // Get the token group information from the access token.

        if (!GetTokenInformation(
                hToken,                              // handle to the access 
token
                TokenGroups,                    // get information about the 
token's groups
                (LPVOID) ptgPrivileges,    // pointer to TOKEN_GROUPS buffer
                dwLength,                          // size of buffer
                &dwLength                        // receives required buffer 
size
                ))
        {
                Status = false;
                goto Cleanup;
        }

        for(unsigned int i = 0; i < ptgPrivileges->PrivilegeCount; i ++ )
        {
                dwPrivilegeNameSize = sizeof ucPrivilegeName;
                dwDisplayNameSize = sizeof ucDisplayName;
                LookupPrivilegeName( NULL, 
&ptgPrivileges->Privileges[i].Luid,
                        ucPrivilegeName, &dwPrivilegeNameSize );
                LookupPrivilegeDisplayName( NULL, ucPrivilegeName,
                        ucDisplayName, &dwDisplayNameSize, &dwLangId );
//                printf( "%40s (%s)\n", ucDisplayName, ucPrivilegeName );
                DisplayName = *ucDisplayName;
                Form1->Memo1->Lines->Add(DisplayName);// + " ---- " + 
ucPrivilegeName);
        }

        if ( !LookupPrivilegeValue( NULL, SE_TCB_NAME, &setcbnameValue ) )
        {
//                puts( "LookupPrivilegeValue" );
                ShowMessage(" LookupPrivilegeValue ");
                Status = false;
                goto Cleanup;
        }

Cleanup:

// Free the buffer for the token groups.

   if (ptg != NULL)
      HeapFree(GetProcessHeap(), 0, (LPVOID)ptg);


return Status;
} 
Re: Both OpenThreadToken & OpenProcessToken Fails
#2259
Author: Bob Gonder
Date: Sun, 18 Mar 2007 00:38
16 lines
1442 bytes
Maurice Anderson wrote:

>The problem is OpenThreadToken fails with "An attempt was made to reference 
>a token that does not exist".  And OpenProcessToken fails with "Access 
>Denied".

Never used this stuff, but from the help, and your errors, this is
what I'd look at:

>        if(!OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, true, &hToken))

I think "true" should be "FALSE" to get the thread token.

>           if(OpenProcessToken(GetCurrentProcess(),

You are reporting an error if it succeeds.
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads