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.

234 lines
5.8 KiB

  1. #include "precomp.h"
  2. //
  3. // Taken from windows\wmi\mofcheck\mofcheck.c
  4. //
  5. //+-------------------------------------------------------------------------
  6. //
  7. // Function: HexStringToDword (private)
  8. //
  9. // Synopsis: scan lpsz for a number of hex digits (at most 8); update lpsz
  10. // return value in Value; check for chDelim;
  11. //
  12. // Arguments: [lpsz] - the hex string to convert
  13. // [Value] - the returned value
  14. // [cDigits] - count of digits
  15. //
  16. // Returns: TRUE for success
  17. //
  18. //--------------------------------------------------------------------------
  19. BOOL HexStringToDword(LPCWSTR lpsz, DWORD * RetValue,
  20. int cDigits, WCHAR chDelim)
  21. {
  22. int Count;
  23. DWORD Value;
  24. Value = 0;
  25. for (Count = 0; Count < cDigits; Count++, lpsz++)
  26. {
  27. if (*lpsz >= '0' && *lpsz <= '9')
  28. Value = (Value << 4) + *lpsz - '0';
  29. else if (*lpsz >= 'A' && *lpsz <= 'F')
  30. Value = (Value << 4) + *lpsz - 'A' + 10;
  31. else if (*lpsz >= 'a' && *lpsz <= 'f')
  32. Value = (Value << 4) + *lpsz - 'a' + 10;
  33. else
  34. return(FALSE);
  35. }
  36. *RetValue = Value;
  37. if (chDelim != 0)
  38. return *lpsz++ == chDelim;
  39. else
  40. return TRUE;
  41. }
  42. //+-------------------------------------------------------------------------
  43. //
  44. // Function: wUUIDFromString (internal)
  45. //
  46. // Synopsis: Parse UUID such as 00000000-0000-0000-0000-000000000000
  47. //
  48. // Arguments: [lpsz] - Supplies the UUID string to convert
  49. // [pguid] - Returns the GUID.
  50. //
  51. // Returns: TRUE if successful
  52. //
  53. //--------------------------------------------------------------------------
  54. BOOL wUUIDFromString(LPCWSTR lpsz, LPGUID pguid)
  55. {
  56. DWORD dw;
  57. if (!HexStringToDword(lpsz, &pguid->Data1, sizeof(DWORD)*2, '-'))
  58. return FALSE;
  59. lpsz += sizeof(DWORD)*2 + 1;
  60. if (!HexStringToDword(lpsz, &dw, sizeof(WORD)*2, '-'))
  61. return FALSE;
  62. lpsz += sizeof(WORD)*2 + 1;
  63. pguid->Data2 = (WORD)dw;
  64. if (!HexStringToDword(lpsz, &dw, sizeof(WORD)*2, '-'))
  65. return FALSE;
  66. lpsz += sizeof(WORD)*2 + 1;
  67. pguid->Data3 = (WORD)dw;
  68. if (!HexStringToDword(lpsz, &dw, sizeof(BYTE)*2, 0))
  69. return FALSE;
  70. lpsz += sizeof(BYTE)*2;
  71. pguid->Data4[0] = (BYTE)dw;
  72. if (!HexStringToDword(lpsz, &dw, sizeof(BYTE)*2, '-'))
  73. return FALSE;
  74. lpsz += sizeof(BYTE)*2+1;
  75. pguid->Data4[1] = (BYTE)dw;
  76. if (!HexStringToDword(lpsz, &dw, sizeof(BYTE)*2, 0))
  77. return FALSE;
  78. lpsz += sizeof(BYTE)*2;
  79. pguid->Data4[2] = (BYTE)dw;
  80. if (!HexStringToDword(lpsz, &dw, sizeof(BYTE)*2, 0))
  81. return FALSE;
  82. lpsz += sizeof(BYTE)*2;
  83. pguid->Data4[3] = (BYTE)dw;
  84. if (!HexStringToDword(lpsz, &dw, sizeof(BYTE)*2, 0))
  85. return FALSE;
  86. lpsz += sizeof(BYTE)*2;
  87. pguid->Data4[4] = (BYTE)dw;
  88. if (!HexStringToDword(lpsz, &dw, sizeof(BYTE)*2, 0))
  89. return FALSE;
  90. lpsz += sizeof(BYTE)*2;
  91. pguid->Data4[5] = (BYTE)dw;
  92. if (!HexStringToDword(lpsz, &dw, sizeof(BYTE)*2, 0))
  93. return FALSE;
  94. lpsz += sizeof(BYTE)*2;
  95. pguid->Data4[6] = (BYTE)dw;
  96. if (!HexStringToDword(lpsz, &dw, sizeof(BYTE)*2, 0))
  97. return FALSE;
  98. lpsz += sizeof(BYTE)*2;
  99. pguid->Data4[7] = (BYTE)dw;
  100. return TRUE;
  101. }
  102. //+-------------------------------------------------------------------------
  103. //
  104. // Function: wGUIDFromString (internal)
  105. //
  106. // Synopsis: Parse GUID such as {00000000-0000-0000-0000-000000000000}
  107. //
  108. // Arguments: [lpsz] - the guid string to convert
  109. // [pguid] - guid to return
  110. //
  111. // Returns: TRUE if successful
  112. //
  113. //--------------------------------------------------------------------------
  114. BOOL wGUIDFromString(LPCWSTR lpsz, LPGUID pguid)
  115. {
  116. DWORD dw;
  117. if (*lpsz == '{' )
  118. lpsz++;
  119. if(wUUIDFromString(lpsz, pguid) != TRUE)
  120. return FALSE;
  121. lpsz +=36;
  122. if (*lpsz == '}' )
  123. lpsz++;
  124. if (*lpsz != '\0') // check for zero terminated string - test bug #18307
  125. {
  126. return FALSE;
  127. }
  128. return TRUE;
  129. }
  130. DWORD
  131. EnablePrivilege(
  132. LPCTSTR pszPrivilege
  133. )
  134. {
  135. DWORD dwError = 0;
  136. BOOL bStatus = FALSE;
  137. HANDLE hTokenHandle = NULL;
  138. TOKEN_PRIVILEGES NewState;
  139. TOKEN_PRIVILEGES PreviousState;
  140. DWORD dwReturnLength = 0;
  141. bStatus = OpenThreadToken(
  142. GetCurrentThread(),
  143. TOKEN_ALL_ACCESS,
  144. TRUE,
  145. &hTokenHandle
  146. );
  147. if (!bStatus) {
  148. bStatus = OpenProcessToken(
  149. GetCurrentProcess(),
  150. TOKEN_ALL_ACCESS,
  151. &hTokenHandle
  152. );
  153. if (!bStatus) {
  154. dwError = GetLastError();
  155. BAIL_ON_WIN32_ERROR(dwError);
  156. }
  157. }
  158. NewState.PrivilegeCount = 1;
  159. NewState.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  160. bStatus = LookupPrivilegeValue(
  161. NULL,
  162. pszPrivilege,
  163. &NewState.Privileges[0].Luid
  164. );
  165. if (!bStatus) {
  166. dwError = GetLastError();
  167. BAIL_ON_WIN32_ERROR(dwError);
  168. }
  169. bStatus = AdjustTokenPrivileges(
  170. hTokenHandle,
  171. FALSE,
  172. &NewState,
  173. sizeof(PreviousState),
  174. &PreviousState,
  175. &dwReturnLength
  176. );
  177. if (!bStatus) {
  178. dwError = GetLastError();
  179. BAIL_ON_WIN32_ERROR(dwError);
  180. }
  181. error:
  182. if (hTokenHandle) {
  183. CloseHandle(hTokenHandle);
  184. }
  185. return (dwError);
  186. }