Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

165 lines
3.4 KiB

  1. /*++
  2. Copyright (C) 1997-2001 Microsoft Corporation
  3. Module Name:
  4. Abstract:
  5. History:
  6. --*/
  7. // Utils.cpp
  8. #include "stdafx.h"
  9. #include "utils.h"
  10. #include <cominit.h>
  11. HRESULT EnableAllPrivileges(DWORD dwTokenType)
  12. {
  13. // Open thread token
  14. // =================
  15. HANDLE hToken = NULL;
  16. BOOL bRes;
  17. switch (dwTokenType)
  18. {
  19. case TOKEN_THREAD:
  20. bRes = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES, TRUE, &hToken);
  21. break;
  22. case TOKEN_PROCESS:
  23. bRes = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES, &hToken);
  24. break;
  25. }
  26. if(!bRes)
  27. return WBEM_E_ACCESS_DENIED;
  28. // Get the privileges
  29. // ==================
  30. DWORD dwLen;
  31. TOKEN_USER tu;
  32. memset(&tu,0,sizeof(TOKEN_USER));
  33. bRes = GetTokenInformation(hToken, TokenPrivileges, &tu, sizeof(TOKEN_USER), &dwLen);
  34. BYTE* pBuffer = new BYTE[dwLen];
  35. if(pBuffer == NULL)
  36. {
  37. CloseHandle(hToken);
  38. return WBEM_E_OUT_OF_MEMORY;
  39. }
  40. bRes = GetTokenInformation(hToken, TokenPrivileges, pBuffer, dwLen,
  41. &dwLen);
  42. if(!bRes)
  43. {
  44. CloseHandle(hToken);
  45. delete [] pBuffer;
  46. return WBEM_E_ACCESS_DENIED;
  47. }
  48. // Iterate through all the privileges and enable them all
  49. // ======================================================
  50. TOKEN_PRIVILEGES* pPrivs = (TOKEN_PRIVILEGES*)pBuffer;
  51. for(DWORD i = 0; i < pPrivs->PrivilegeCount; i++)
  52. {
  53. pPrivs->Privileges[i].Attributes |= SE_PRIVILEGE_ENABLED;
  54. }
  55. // Store the information back into the token
  56. // =========================================
  57. bRes = AdjustTokenPrivileges(hToken, FALSE, pPrivs, 0, NULL, NULL);
  58. delete [] pBuffer;
  59. CloseHandle(hToken);
  60. if(!bRes)
  61. return WBEM_E_ACCESS_DENIED;
  62. else
  63. return WBEM_S_NO_ERROR;
  64. }
  65. BOOL GetFileVersion(LPCTSTR szFN, LPTSTR szVersion)
  66. {
  67. DWORD dwCount,
  68. dwHandle,
  69. dwValueLen;
  70. BOOL bRet;
  71. char *pcValue,
  72. *pc,
  73. *pBuffer,
  74. szFileName[MAX_PATH],
  75. szQuery[100];
  76. lstrcpy(szFileName, szFN);
  77. if ((dwCount = GetFileVersionInfoSize(szFileName, &dwHandle)) != 0)
  78. {
  79. pBuffer = new char[dwCount];
  80. if (!pBuffer)
  81. return FALSE;
  82. if (GetFileVersionInfo(szFileName, dwHandle, dwCount, pBuffer) != 0)
  83. {
  84. VerQueryValue(pBuffer, "\\VarFileInfo\\Translation",
  85. (void **) &pcValue, (UINT *) &dwValueLen);
  86. if (dwValueLen != 0)
  87. {
  88. wsprintf(szQuery, "\\StringFileInfo\\%04X%04X\\FileVersion",
  89. *(WORD *)pcValue, *(WORD *)(pcValue+2));
  90. bRet = VerQueryValue(pBuffer, szQuery, (void **) &pcValue,
  91. (UINT *) &dwValueLen);
  92. if (bRet)
  93. {
  94. while ((pc = strchr(pcValue, '(')) != NULL)
  95. *pc = '{';
  96. while ((pc = strchr(pcValue, ')')) != NULL)
  97. *pc = '}';
  98. _tcscpy(szVersion, pcValue);
  99. delete pBuffer;
  100. return TRUE;
  101. }
  102. }
  103. }
  104. delete pBuffer;
  105. }
  106. return FALSE;
  107. }
  108. void SetSecurityHelper(
  109. IUnknown *pUnk,
  110. BSTR pAuthority,
  111. BSTR pUser,
  112. BSTR pPassword,
  113. DWORD dwImpLevel,
  114. DWORD dwAuthLevel)
  115. {
  116. BSTR pPrincipal = NULL;
  117. COAUTHIDENTITY *pAuthIdentity = NULL;
  118. SetInterfaceSecurityEx(
  119. pUnk,
  120. pAuthority,
  121. pUser,
  122. pPassword,
  123. dwAuthLevel,
  124. dwImpLevel,
  125. EOAC_NONE,
  126. &pAuthIdentity,
  127. &pPrincipal);
  128. if (pPrincipal)
  129. SysFreeString(pPrincipal);
  130. if (pAuthIdentity)
  131. WbemFreeAuthIdentity(pAuthIdentity);
  132. }