Leaked source code of windows server 2003
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.

339 lines
8.5 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. }
  187. BOOL
  188. IsStringInArray(
  189. LPWSTR * ppszStrings,
  190. LPWSTR pszKey,
  191. DWORD dwNumStrings
  192. )
  193. {
  194. DWORD j = 0;
  195. for (j = 0; j < dwNumStrings; j++) {
  196. if (!_wcsicmp(*(ppszStrings+j), pszKey)) {
  197. return TRUE;
  198. }
  199. }
  200. return FALSE;
  201. }
  202. // Description:
  203. //
  204. // Pastore function that uses above time routine to convert
  205. // from LDAP UTC Generalized time coding to time_t
  206. // Generalized time syntax has the form
  207. // YYYYMMDDHHMMSS[.|,fraction][(+|-HHMM)|Z]
  208. // Z means UTC.
  209. //
  210. //
  211. // Arguments:
  212. //
  213. // pszGenTime: UTC time in generalized time format as above.
  214. // ptTime: Converted time in time_t format.
  215. //
  216. // Assumptions:
  217. // Always assume UTC and never expect or check (+|-HHMM)
  218. // since DC time returned is in UTC.
  219. //
  220. // Return value:
  221. // WIN32 error or ERROR_SUCCESS
  222. DWORD
  223. GeneralizedTimeToTime(
  224. IN LPWSTR pszGenTime,
  225. OUT time_t * ptTime
  226. )
  227. {
  228. DWORD dwError = ERROR_SUCCESS;
  229. DWORD dwLen = 0;
  230. DWORD dwNumFields = 0;
  231. DWORD dwTrueYear = 0;
  232. struct tm tmTimeStruct;
  233. time_t tTime = 0;
  234. dwLen = wcslen(pszGenTime);
  235. if (dwLen < MIN_GEN_UTC_LEN ||
  236. !( pszGenTime[MIN_GEN_UTC_LEN-1] == L'.' ||
  237. pszGenTime[MIN_GEN_UTC_LEN-1] == L','))
  238. {
  239. dwError = ERROR_INVALID_PARAMETER;
  240. BAIL_ON_WIN32_ERROR(dwError);
  241. }
  242. // First convert to tm struct format
  243. //
  244. memset(&tmTimeStruct, 0, sizeof(struct tm));
  245. dwNumFields = _snwscanf(
  246. pszGenTime, MIN_GEN_UTC_LEN, L"%04d%02d%02d%02d%02d%02d",
  247. &dwTrueYear, &tmTimeStruct.tm_mon, &tmTimeStruct.tm_mday,
  248. &tmTimeStruct.tm_hour, &tmTimeStruct.tm_min, &tmTimeStruct.tm_sec
  249. );
  250. if (dwNumFields != 6 || dwTrueYear < 1900)
  251. {
  252. dwError = ERROR_INVALID_PARAMETER;
  253. BAIL_ON_WIN32_ERROR(dwError);
  254. }
  255. tmTimeStruct.tm_year = dwTrueYear - 1900;
  256. // Month is zero-based so...
  257. //
  258. if (tmTimeStruct.tm_mon <= 0)
  259. {
  260. dwError = ERROR_INVALID_PARAMETER;
  261. BAIL_ON_WIN32_ERROR(dwError);
  262. }
  263. tmTimeStruct.tm_mon--;
  264. // Now convert to time_t
  265. //
  266. tTime = _mkgmtime(&tmTimeStruct);
  267. if (tTime == (time_t) -1)
  268. {
  269. dwError = ERROR_INVALID_PARAMETER;
  270. BAIL_ON_WIN32_ERROR(dwError);
  271. }
  272. *ptTime = tTime;
  273. return dwError;
  274. error:
  275. return dwError;
  276. }