home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 March / CMCD0304.ISO / Software / Freeware / Programare / nullsoft / nsis20.exe / Contrib / UserInfo / UserInfo.c next >
C/C++ Source or Header  |  2003-11-12  |  4KB  |  119 lines

  1. #include <windows.h>
  2. #include "..\exdll\exdll.h"
  3.  
  4. void __declspec(dllexport) GetName(HWND hwndParent, int string_size, 
  5.                                    char *variables, stack_t **stacktop)
  6. {
  7.   EXDLL_INIT();
  8.  
  9.   {
  10.     DWORD dwStringSize = g_stringsize;
  11.     stack_t *th;
  12.     if (!g_stacktop) return;
  13.     th = (stack_t*) GlobalAlloc(GPTR, sizeof(stack_t) + g_stringsize);
  14.     GetUserName(th->text, &dwStringSize);
  15.     th->next = *g_stacktop;
  16.     *g_stacktop = th;
  17.   }
  18. }
  19.  
  20. void __declspec(dllexport) GetAccountType(HWND hwndParent, int string_size, 
  21.                                           char *variables, stack_t **stacktop)
  22. {
  23.   EXDLL_INIT();
  24.  
  25.   {
  26.     HANDLE        hThread;
  27.     TOKEN_GROUPS  *ptg = NULL;
  28.     DWORD         cbTokenGroups;
  29.     DWORD         i, j;
  30.  
  31.     SID_IDENTIFIER_AUTHORITY SystemSidAuthority = SECURITY_NT_AUTHORITY;
  32.  
  33.     char *group = "";
  34.  
  35.     if (GetVersion() & 0x80000000) // Not NT
  36.     {
  37.       group = "Admin";
  38.     }
  39.  
  40.     // First we must open a handle to the access token for this thread.
  41.  
  42.     else if (OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, FALSE, &hThread) ||
  43.         OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hThread))
  44.     {
  45.       // Then we must query the size of the group information associated with
  46.       // the token. Note that we expect a FALSE result from GetTokenInformation
  47.       // because we've given it a NULL buffer. On exit cbTokenGroups will tell
  48.       // the size of the group information.
  49.  
  50.       if (!GetTokenInformation (hThread, TokenGroups, NULL, 0, &cbTokenGroups) &&
  51.           GetLastError() == ERROR_INSUFFICIENT_BUFFER)
  52.       {
  53.  
  54.         // Now we allocate a buffer for the group information.
  55.         // Since _alloca allocates on the stack, we don't have
  56.         // to explicitly deallocate it. That happens automatically
  57.         // when we exit this function.
  58.  
  59.         if (ptg = GlobalAlloc(GPTR, cbTokenGroups))
  60.         {
  61.  
  62.           // Now we ask for the group information again.
  63.           // This may fail if an administrator has added this account
  64.           // to an additional group between our first call to
  65.           // GetTokenInformation and this one.
  66.  
  67.           if (GetTokenInformation(hThread, TokenGroups, ptg, cbTokenGroups, &cbTokenGroups))
  68.           {
  69.  
  70.             struct group
  71.             {
  72.               DWORD auth_id;
  73.               char *name;
  74.             } groups[] = {
  75.               {DOMAIN_ALIAS_RID_USERS, "User"},
  76.               // every user belongs to the users group, hence users comes before guests
  77.               {DOMAIN_ALIAS_RID_GUESTS, "Guest"},
  78.               {DOMAIN_ALIAS_RID_POWER_USERS, "Power"},
  79.               {DOMAIN_ALIAS_RID_ADMINS, "Admin"}
  80.             };
  81.  
  82.             // Finally we'll iterate through the list of groups for this access
  83.             // token looking for a match against the SID we created above.
  84.  
  85.             for (i = 0; i < sizeof(groups)/sizeof(struct group); i++)
  86.             {
  87.               PSID psid = 0;
  88.               AllocateAndInitializeSid(
  89.                 &SystemSidAuthority,
  90.                 2,
  91.                 SECURITY_BUILTIN_DOMAIN_RID,
  92.                 groups[i].auth_id,
  93.                 0, 0, 0, 0, 0, 0,
  94.                 &psid
  95.               );
  96.               if (psid == 0) continue;
  97.               for (j = 0; j < ptg->GroupCount; j++)
  98.                 if (EqualSid(ptg->Groups[j].Sid, psid))
  99.                   group = groups[i].name;
  100.               FreeSid(psid);
  101.             }
  102.           }
  103.  
  104.           GlobalFree(ptg);
  105.         }
  106.       }
  107.  
  108.       CloseHandle(hThread);
  109.     }
  110.  
  111.     pushstring(group);
  112.   }
  113. }
  114.  
  115. BOOL WINAPI DllMain(HANDLE hInst, ULONG ul_reason_for_call, LPVOID lpReserved)
  116. {
  117.     return TRUE;
  118. }
  119.